[英]PHP Curl to C# Webclient/HTTPRequest
我正在嘗試將以下代碼轉換為C#,但運氣不佳。
<?php
$data = '<xml>my xml request here</xml>';
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "http://myendpoint");
curl_setopt($tuCurl, CURLOPT_PORT , 443);
curl_setopt($tuCurl, CURLOPT_VERBOSE, 0);
curl_setopt($tuCurl, CURLOPT_HEADER, 0);
curl_setopt($tuCurl, CURLOPT_POST, 1);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);
curl_setopt($tuCurl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".strlen($data)));
以下是我提出的內容,但未收到預期的結果。 我的C#中缺少什么嗎?
using (var client = new System.Net.WebClient())
{
var data = new NameValueCollection();
data.Add("", "my xml here");
var result = client.UploadValues("my endpoint here", data);
var str = Encoding.UTF8.GetString(result);
}
僅供參考,我用以下代碼段弄清了這一點:
public static string POST(string url, string requestXML)
{
WebRequest webReq = HttpWebRequest.Create(url);
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.Method = "POST";
using (var stream = webReq.GetRequestStream())
{
var arrBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(requestXML);
stream.Write(arrBytes, 0, arrBytes.Length);
stream.Close();
}
WebResponse webResp = webReq.GetResponse();
var respStream = webResp.GetResponseStream();
var reader = new StreamReader(respStream, System.Text.Encoding.ASCII);
string strResponse = reader.ReadToEnd();
respStream.Close();
return strResponse;
}
http://vh4u.blogspot.com/2015/02/using-dhl-web-services-in-your-net.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.