HttpWebRequest.EndGetRequestStream issue in Silverlight

I use HTTPWebRequest to send data to a web server in Silverlight 3.0, here is my code

    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();

}

The problem I am facing is that the line Stream postStream = request.EndGetRequestStream(asynchronousResult);makes some blocking call, and my whole application seems to be hung. However, I can open the same web page from a browser ... Why is this so?

+3
source share
4 answers

Remove the use of the wait descriptor allDone. Instead, transfer your call BeginGetResponseto the end of the method ReadCallback. In fact, you are connecting calls: -

BeginGetRequest->ReadCallback->BeginGetResponse->ResponseCallback

, Content-Type "application/x-www-form-urlencoded". Multipart, "multipart/form-data".

+3

, , :

 allDone.WaitOne();

, .

0

, , POST / , WebClient. , .

, , , , .

0

it seems or maybe you will get access to a URL that requires "permission". A cookie is stored in the web browser or any of them for your permission to access, but your application is not. Once you access this URL using a browser, you will only get this access permission for your browser application and you cannot get another application.

0
source

Source: https://habr.com/ru/post/1723036/


All Articles