c# - Silverlight HTTP POST 几个变量,最简单的示例(最少代码)

标签 c# silverlight http

您好,我想将一些数据从 silverlight 发布到网站。
我发现以下 link并且有效...
但是....这个例子太复杂了,我看得眼睛都疼了。
另外.. flex 示例更清晰/代码更少..

我会说一定有更好的解决方案......

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

链接中的解决方案:

   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 发送表单数据。如果您不关心成功确认,它将非常短:

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

关于c# - Silverlight HTTP POST 几个变量,最简单的示例(最少代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3493921/

相关文章:

c# - 通过 Web 服务将数据从 Sql Server 移动到 Excel

Silverlight与Adobe Air

silverlight - 无法从文本中创建 ‘System.Windows.Input.ICommand’

http - 用于实现 HTTP 服务器的 RFC

HTTP 推送超过 100,000 个连接

C#多态简单题

c# - Foreach 循环将迭代变量传递给带有和不带 ref 的方法 - 不明白为什么它不允许使用 ref

c# - 计算百分比时抛出 OverflowException

c# - 在 silverlight 问题中绑定(bind)到 List<object>

http - netstat 上的奇怪连接