其他分享
首页 > 其他分享> > 在Silverlight中的HttpWebRequest.EndGetRequestStream问题

在Silverlight中的HttpWebRequest.EndGetRequestStream问题

作者:互联网

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

    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

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

标签:post,httpwebrequest,silverlight-3-0,c,silverlight
来源: https://codeday.me/bug/20191210/2101526.html