Silverlight的HTTP POST几个变量,最简单的例子(最少的代码) [英] Silverlight HTTP POST few variables, SIMPLEST example (least code)
问题描述
您好我想从Silverlight的一些数据发布到一个网站。结果
我发现下面的链接和它的作品...结果
但是.... 这个例子是如此拟订该让我的眼睛受伤了。结果
还..柔性例子是干净多了/少代码..结果
结果
我说必须有一个更好的解决方案..
有关参考。我们发布2个变量(串),并读出结果(字符串)。
从链路解决方案:
1 // C#
2. //创建一个请求对象
3. HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(新的URI(POST_ADDRESS,UriKind.Absolute));
4. request.Method =POST;
5 //千万不要错过了这个
6. request.ContentType =应用/的X WWW的形式,进行了urlencoded
7. request.BeginGetRequestStream(新的AsyncCallback(RequestReady),请求);
8
9. //透过POST数据
10.无效RequestReady(IAsyncResult的asyncResult)
11. {
12. HttpWebRequest的要求= asyncResult.AsyncState作为HttpWebRequest的;
13.流流= request.EndGetRequestStream(asyncResult);
14,
15. //哈克解决多线程问题
16. //我觉得这是一个错误
17. this.Dispatcher.BeginInvoke(委托()
18. {
19. //发送POST变量
20. StreamWriter的作家=新的StreamWriter(流);
21. writer.WriteLine(键1 =值) ;
22. writer.WriteLine(键2 =值);
23 writer.Flush();
24 writer.Close();
25
26. request.BeginGetResponse(新的AsyncCallback(ResponseReady),请求);
27.});
28}
29
30. //得到的结果
31.无效ResponseReady(IAsyncResult的asyncResult)
32. {
33的HttpWebRequest要求= asyncResult.AsyncState为HttpWebRequest的;
34. HttpWebResponse响应=(HttpWebResponse)request.EndGetResponse(asyncResult);
35
36. this.Dispatcher.BeginInvoke(委托()
37. {
38.流responseStream = response.GetResponseStream();
39的StreamReader读者=新的StreamReader(responseStream);
40. //得到的结果文本
41.字符串结果= reader.ReadToEnd();
42.});
43}
您可以使用Web客户端发送表单数据。如果你不关心的成功确认这将是非常短暂:
WebClient的WC =新的WebClient();
wc.Headers [内容类型] =应用/的X WWW的形式,进行了urlencoded
wc.UploadStringAsync(新的URI(postUrl),POST,VAL1 =参数1&放大器;将val2 =参数2);
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. }
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屋!