Silverlight HTTP POST 几个变量,最简单的例子(最少的代码) [英] Silverlight HTTP POST few variables, SIMPLEST example (least code)

查看:15
本文介绍了Silverlight HTTP POST 几个变量,最简单的例子(最少的代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想将一些来自 Silverlight 的数据发布到网站.
我发现了以下 link 和它有效...
然而....这个例子太详细了,让我眼睛疼.
另外.. flex 示例更简洁/更少代码..

我想说必须有更好的解决方案...

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...

供参考..我们发布2个变量(字符串)并读出结果(字符串).

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

来自链接的解决方案:

   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. }  

推荐答案

您可以使用 WebClient 发送表单数据.如果你不关心成功的确认,它会很短:

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

这篇关于Silverlight HTTP POST 几个变量,最简单的例子(最少的代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆