What is the equivalent way to set message options in .net?

I need to integrate with a third-party API. To use this service, I have to "POST" to a specific URL with specific parameters.

Sample code provided by the service is in php and looks like this

$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); 

I am trying to use the WebRequest class to achieve the same in .net. However, I am a bit confused about how to set the post parameters data. I realized that the $ data above is nothing more than a dictionary. So I created an equivalent dictionary. However, how to set message parameters with dictionary values?

At http://msdn.microsoft.com/en-us/library/debx8sh9.aspx they serialize the string into an array of bytes, and then set it as a post parameter to the dataStream. How do I do the same for a dictionary?

Or is my approach wrong? Is there a better way to do this?

+6
source share
4 answers

Typically, WebClient.UploadValues will be the easiest way; see MSDN for a complete example . Please note, however, that this only covers CURLOPT_POSTFIELDS and CURLOPT_POST . Error on error is automatic and implicit, and the response is already included as byte[] .

i.e.

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

note POST is implied here; if you need another http method use overload:

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

If you use url encoded mail data, you can encode each key / value pair of your dictionary with the 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); } 
+2
source

You probably want to use

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
source

Just in case, you should use HttpWebRequest, the code below converts the Dictionary<String,String> with the name formVars to byte[] with the name toPost :

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

Play with a working copy at https://dotnetfiddle.net/IOzIE6

0
source

Source: https://habr.com/ru/post/898927/


All Articles