小编gru*_*mlu的帖子

从HttpWebRequest.BeginGetResponse回调中获取数据

我正在编写一个Windows Phone 8应用程序,它应该向服务器发送GET + POST请求并解析答案.

我用来发送请求并获得响应的代码如下(它是在一个单独的静态类中编写的):

                // server to POST to
        string url = "http://myserver.com/?page=hello&param2=val2";

        // HTTP web request
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/x-www-form-urlencoded";
        httpWebRequest.Method = "POST";

        // Write the request Asynchronously 
        using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream,
                                                                 httpWebRequest.EndGetRequestStream, null))
        {
            // Create the post data
            string postData = "pseudo=pseudo&titre=test&texte=\"Contenu du message\"";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Write the bytes to the stream
            await stream.WriteAsync(byteArray, 0, byteArray.Length);
            stream.Close();
            IAsyncResult ar = httpWebRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), httpWebRequest);

        }



    }

    private static …
Run Code Online (Sandbox Code Playgroud)

.net c# callback httpwebrequest windows-phone-8

2
推荐指数
1
解决办法
3万
查看次数

标签 统计

.net ×1

c# ×1

callback ×1

httpwebrequest ×1

windows-phone-8 ×1