将PHP cURL转换为C#请求

时间:2013-02-15 14:05:52

标签: c# php .net curl

我在PHP中有以下代码,它返回一个XML文件,效果很好。我的问题是我需要使用C#实现相同的功能。由于我是.NET的新手,有人能指出我正确的方向吗?

$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 个答案:

答案 0 :(得分: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;
        }