PHP 卷曲到 C# Webclient/HTTPRequest
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
我正在尝试将以下内容转换为 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