Silverlight HTTP POST few variables, SIMPLEST example (least code)

孤街浪徒 提交于 2019-12-18 16:52:45

问题


Hello I want to post some data from silverlight to a website.
I found the following link and it works...
However.... This example was so elaborate it made my eyes hurt.
Also.. the flex example was much cleaner/less code..

I'd say there must be a better solution...

For reference.. We post 2 variables (strings) and read out the result (string).

The solution from the link :

   1. // C#  
   2. // Create a request object  
   3. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(POST_ADDRESS, UriKind.Absolute));  
   4. request.Method = "POST";  
   5. // don't miss out this  
   6. request.ContentType = "application/x-www-form-urlencoded";  
   7. request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);  
   8.   
   9. // Sumbit the Post Data  
  10. void RequestReady(IAsyncResult asyncResult)  
  11. {  
  12.     HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;  
  13.     Stream stream = request.EndGetRequestStream(asyncResult);  
  14.   
  15.     // Hack for solving multi-threading problem  
  16.     // I think this is a bug  
  17.     this.Dispatcher.BeginInvoke(delegate()  
  18.     {  
  19.         // Send the post variables  
  20.         StreamWriter writer = new StreamWriter(stream);  
  21.         writer.WriteLine("key1=value1");  
  22.         writer.WriteLine("key2=value2");  
  23.         writer.Flush();  
  24.         writer.Close();  
  25.   
  26.         request.BeginGetResponse(new AsyncCallback(ResponseReady), request);  
  27.     });  
  28. }  
  29.   
  30. // Get the Result  
  31. void ResponseReady(IAsyncResult asyncResult)  
  32. {  
  33.     HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;  
  34.     HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);  
  35.   
  36.     this.Dispatcher.BeginInvoke(delegate()  
  37.     {  
  38.         Stream responseStream = response.GetResponseStream();  
  39.         StreamReader reader = new StreamReader(responseStream);  
  40.     // get the result text  
  41.         string result = reader.ReadToEnd();  
  42.     });  
  43. }  

回答1:


You can use WebClient to send form data. If you don't care about confirmation of success it will be very short:

WebClient wc = new WebClient();
wc.Headers["Content-type"] = "application/x-www-form-urlencoded";
wc.UploadStringAsync(new Uri(postUrl), "POST", "val1=param1&val2=param2");



回答2:


What part especially hurt you eyes? Less code? You can wrap all this in one helper class with event, and you will have same lines count as sample on AS. And there is no flex sample, there is AS3 sample =). AS3 variant is same, just wrapped(by adobe) as single class, that have outside just one callback. And also i want to remind you, this is not - old good synchronous request, this is asynchronous and it's always so ugly (IMHO). And there is no synchronous network in silverlight, so i think you should used to it.



来源:https://stackoverflow.com/questions/3493921/silverlight-http-post-few-variables-simplest-example-least-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!