2011-10-09 53 views
6

我必須與第三方API集成。要使用該服務,我必須「POST」到具有特定參數的特定網址。在.net中設置發佈參數的等效方法是什麼?

服務所提供的示例代碼是在PHP是如下

$data = array('From' => '0999999', 'To' => '08888888'); 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_FAILONERROR, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); <--- Ignore SSL warnings 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 

我試圖使用WebRequest類來實現.NET一樣。但是,我對如何設置後期參數數據有點困惑。我認爲上面的$數據不過是一個字典。所以我創建了一個等價的字典。但是,如何使用字典值設置發佈參數?

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx中,他們已經將字符串序列化爲一個字節數組,然後將其設置爲dataStream中的post參數。我如何對字典做同樣的事情?

或者我的方法不正確?有一個更好的方法嗎?

回答

8

一般來說,WebClient.UploadValues是要在這裏是最簡單的方法後的數據,你可以URL編碼每個鍵/值對你的字典中; see MSDN for a full example。但請注意,這僅涵蓋CURLOPT_POSTFIELDSCURLOPT_POST。錯誤失敗是自動和隱含的,並且響應已經包含在byte[]中。

using(var client = new WebClient()) { 
    var data = new NameValueCollection(); 
    data.Add("From", "0999999"); 
    data.Add("To", "08888888"); 
    var result = client.UploadValues(url, data); 
} 

POST是這裏隱含的;如果你需要一個不同的HTTP方法,使用過載:

var result = client.UploadValues(url, "PUT", data); // for example 
+0

是否有相當於CURLOPT_SSL_VERIFYPEER的值,以便我可以忽略任何SSL異常?遠程證書是自己生成的SSL證書,第三方Api開發人員希望其他人忽略任何SSL警告。或者,在http://www.ben-morris.com/asp-net-web-services-and-ssl-certificates-establishing-a-trust-relationship上顯示的方法是最簡單的方法嗎?這應該是一個單獨的問題嗎? – shashi

+1

@sassyboy重新SSL,請參閱http://stackoverflow.com/questions/109186/bypass-invalid-ssl-certificate-errors-when-calling-web-services-in-net/865786#865786 –

+0

謝謝。這就是訣竅! – shashi

2

如果您使用的URL編碼使用HttpServerUtility.UrlEncode Method (String)

// Build postData 
StringBuilder post = new StringBuilder(); 
foreach(item in dictionary) { 
    post.AppendFormat("&{0}={1}", item.key, HttpUtility.UrlEncode(item.value)); 
} 
string postData = post.ToString(); 

// HTTP POST 
Uri uri = new Uri(url); 
request = (HttpWebRequest) WebRequest.Create(uri); 
request.Method = "POST"; 
request.ContentType = "application/x-www-form-urlencoded"; 
request.ContentLength = postData.Length; 
using(Stream writeStream = request.GetRequestStream()) 
{ 
    UTF8Encoding encoding = new UTF8Encoding(); 
    byte[] bytes = encoding.GetBytes(postData); 
    writeStream.Write(bytes, 0, bytes.Length); 
} 
+1

錯字上第二行玩有一個工作拷貝。拼寫錯誤的StringBuilder。 –

+0

@JavaScriptDude更正 –

0

你可能想使用

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://url"); 
request.AllowWriteStreamBuffering = true; 
request.Method = "POST"; 
string post = "From=0999999&To=08888888"; 
request.ContentLength = post.Length; 
request.ContentType = "application/x-www-form-urlencoded"; 

StreamWriter writer = new StreamWriter(request.GetRequestStream()); 
writer.Write(post); 
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
0

萬一你使用HttpWebRequest的,代碼將名爲formVarsDictionary<String,String>轉換爲名爲toPostbyte[]

byte[] toPost = System.Text.Encoding.UTF8.GetBytes(
    String.Join("&", formVars.Select(x => 
     HttpUtility.UrlEncode(x.Key) + "=" + 
     HttpUtility.UrlEncode(x.Value))); 
); 

https://dotnetfiddle.net/IOzIE6

相關問題