c# - Silverlight 中的 HttpWebRequest.EndGetRequestStream 问题

标签 c# silverlight silverlight-3.0 post httpwebrequest

我正在使用 HTTPWebRequest 将数据发布到 Silver light 3.0 中的网络服务器,这是我的代码

    public void MakePostRequest()
    {


            // Create a new HttpWebRequest object.

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.mywebsite.com/somepage.php");    

            // Set the ContentType property. 
            request.ContentType="application/x-www-form-urlencoded";
            // Set the Method property to 'POST' to post data to the URI.
            request.Method = "POST";

            // Start the asynchronous operation.    
            request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);    

           // Keep the main thread from continuing while the asynchronous
            // operation completes. A real world application
            // could do something useful such as updating its user interface. 
            allDone.WaitOne();

            // Get the response.

            request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);    
    }


    private static void ResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        Stream streamResponse = resp.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        string responseString = streamRead.ReadToEnd();
        Console.WriteLine(responseString);
        // Close the stream object.
        streamResponse.Close();
        streamRead.Close();

        // Release the HttpWebResponse.
        resp.Close();
    }

    private static void ReadCallback(IAsyncResult asynchronousResult)
    {    
            IDictionary<string, string> objDictionary = new Dictionary<string, string>();  
            objDictionary.Add("action", "login");
            objDictionary.Add("login", "myid@yahoo.com");
            objDictionary.Add("password", "pass123");


            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            // End the operation.
            // This line of code making the blocking call???
            Stream postStream = request.EndGetRequestStream(asynchronousResult);

        string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

            Stream memStream = new System.IO.MemoryStream();

            byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");

            string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

            foreach (KeyValuePair<string, string> entry in objDictionary)
            {
                string key = entry.Key;
               string value = entry.Value;
                string formitem = string.Format(formdataTemplate, key, value);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                memStream.Write(formitembytes, 0, formitembytes.Length);
            }

        memStream.Write(boundarybytes, 0, boundarybytes.Length);



        memStream.Position = 0;
        byte[] tempBuffer = new byte[memStream.Length];
        memStream.Read(tempBuffer, 0, tempBuffer.Length);

        //Writing the name value pair
        postStream.Write(tempBuffer, 0, tempBuffer.Length);

        memStream.Close();
        postStream.Close();

}

我面临的问题是行 Stream postStream = request.EndGetRequestStream(asynchronousResult); 正在进行一些阻塞调用,我的整个应用程序似乎被挂起..但是我可以打开来自浏览器的相同网页...为什么会这样?

最佳答案

删除您对 Wait 句柄 allDone 的使用。而是将对 BeginGetResponse 的调用移动到 ReadCallback 方法的末尾。实际上,您将调用链接起来:-

BeginGetRequest->ReadCallback->BeginGetResponse->ResponseCallback

顺便说一句,您正在使用“application/x-www-form-urlencoded”的内容类型。但是,您似乎正在尝试对 Multipart 实体主体进行编码,在这种情况下,您的内容类型应为“multipart/form-data”。

关于c# - Silverlight 中的 HttpWebRequest.EndGetRequestStream 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1744870/

相关文章:

c# - Linq 均匀分组

silverlight - PagedCollectionView自定义排序

silverlight - 您使用什么来测试 Silverlight 应用程序?

silverlight - 在 Silverlight 中,我如何找到绑定(bind)到有错误的模型的第一个字段,以便我可以给它焦点?

silverlight - x :name and name for controls in xaml file? 有什么区别吗

c# - 如何使用 Office Interop 在 PowerPoint 的自定义任务 Pane 中创建形状并将该形状添加到类别中

c# - 在给定用户已经有 cookie 的情况下启动应用程序时处理 null HttpContext.Current

silverlight - 如何将焦点放在 DataForm 中的 TextBox?

c# - 按下开始按钮时如何处理事件(Windows Phone)

c# - ASP.NET MVC : Requested registry access denied when creating performance counters