发送POST数据

时间:2011-08-13 08:43:46

标签: post windows-phone windows-phone-7.1

我正在尝试连接HTTP服务器并发送一些日期。我的代码看起来像 - >

public MainPage()
{
    InitializeComponent();
    WebClient client = new WebClient();
    Uri uri = new Uri("http://google.pl");
    string data = "Time = 12:00am temperature = 50";
    client.UploadStringCompleted += new UploadStringCompletedEventHandler    (UploadStringCallback2);
    client.UploadStringAsync(uri, data);
}

private static void UploadStringCallback2(Object sender, UploadStringCompletedEventArgs e)
{
    string reply = (string)e.Result;
    Console.WriteLine(reply);
}

我收到异常“远程服务器返回错误:NotFound。”我的调试窗口看起来像

System.Windows.dll中出现'System.Net.WebException'类型的第一次机会异常 System.Windows.dll中发生了'System.Net.WebException'类型的第一次机会异常 System.dll

中发生了'System.Net.WebException'类型的第一次机会异常

请帮忙! PS:我已经安装了7.1 SDK Beta但它应该在7.0模拟器上运行(Target windows phone verison是WP7)。

修改

现在代码看起来像

Uri uri = new Uri("MY SITE");
string data = "text=dupa";
//client.Encoding = System.Text.Encoding.UTF8;
var headers = new WebHeaderCollection();
headers[0] = " User-Agent: CERN-LineMode/2.15 libwww/2.17b3";
client.Headers = headers;
client.UploadStringCompleted += new UploadStringCompletedEventHandler(UploadStringCallback2);
client.UploadStringAsync(uri, data);

它连接到我的PHP脚本

<?php
    print_r($_POST);
    print_r($_SERVER[HTTP_USER_AGENT]);
?>

但回应就像

Array
(
)
NativeHost

提前感谢您的帮助:)

修改 好吧,我想通了;)一切正常;)

3 个答案:

答案 0 :(得分:1)

Google不接受POST请求。我不知道WP7,但如果你用相同的代码创建一个简单的控制台应用程序,你会得到一个不允许使用post方法的例外。

答案 1 :(得分:0)

试用这个简化HTTP调用的类: https://mytoolkit.codeplex.com/wikipage?title=Http(链接到我的codeplex项目)

用法:

var request = new PostRequest("http://myurl.ch");
request.Data.Add("name", "myname");
request.Data.Add("email", "myemail);
Http.Post(request, OnSendCompleted, Deployment.Current.Dispatcher);

OnSendCompleted是在HTTP调用完成后调用的方法。通过这个类,它也很容易发送文件。

答案 2 :(得分:0)

WebClient类在内部使用GET方法,您必须使用HttpWebRequest和HttpWebResponse类。 这是一个小片段

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.foo.com");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        string postData = "parameter=value";
        // Getting the request stream.
        request.BeginGetRequestStream
            (result =>
                    {
                        // Sending the request.
                        using (var requestStream = request.EndGetRequestStream(result))
                        {
                            using (StreamWriter writer = new StreamWriter(requestStream))
                            {
                                writer.Write(postData);
                                writer.Flush();
                            }
                        }

                        // Getting the response.
                        request.BeginGetResponse(responseResult =>
                                                    {
                                                        var webResponse = request.EndGetResponse(responseResult);
                                                        using (var responseStream = webResponse.GetResponseStream() )
                                                        {
                                                            using (var streamReader = new StreamReader(responseStream))
                                                            {
                                                                var result = streamReader.ReadToEnd();

                                                            }
                                                        }                                                         
                                                    }, null);
                    }, null);