2013-02-15 225 views
0

我在PHP中有以下代碼,它返回一個XML文件並且效果很好。我的問題是,我需要使用C#實現相同的功能。由於我對.NET相當陌生,有人能指出我的方向嗎?將PHP cURL轉換爲C#請求

$url = "http://myDestinationDomain.com"; 
$ch = curl_init($url); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true); 
$temp = curl_exec($ch); 
curl_close($ch); 

回答

1

試一試這段代碼。它可能有一些錯誤,但你應該能夠在其上工作了

我從這個鏈接 http://forums.asp.net/t/1178426.aspx/1

public static XmlDocument getXMLDocumentFromXMLTemplate(string inURL) 
     { 
      HttpWebRequest myHttpWebRequest = null;  //Declare an HTTP-specific implementation of the WebRequest class. 
      HttpWebResponse myHttpWebResponse = null; //Declare an HTTP-specific implementation of the WebResponse class 
      XmlDocument myXMLDocument = null;   //Declare XMLResponse document 
      XmlTextReader myXMLReader = null;   //Declare XMLReader 

      try 
      { 
       //Create Request 
       myHttpWebRequest = (HttpWebRequest) HttpWebRequest.Create(inURL); 
       myHttpWebRequest.Method = "GET"; 
       myHttpWebRequest.ContentType = "text/xml; encoding='utf-8'"; 

       //Get Response 
       myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse(); 

       //Now load the XML Document 
       myXMLDocument = new XmlDocument(); 

       //Load response stream into XMLReader 
       myXMLReader = new XmlTextReader(myHttpWebResponse.GetResponseStream()); 
       myXMLDocument.Load(myXMLReader); 
      } 
      catch (Exception myException) 
      { 
       throw new Exception("Error Occurred in AuditAdapter.getXMLDocumentFromXMLTemplate()", myException); 
      } 
      finally 
      { 
       myHttpWebRequest = null; 
       myHttpWebResponse = null; 
       myXMLReader = null; 
      } 
      return myXMLDocument; 
     } 
+0

感謝Murtuza得到它。我在你的幫助下計算出來了。 – Jeagr 2013-02-19 04:27:29