简体   繁体   中英

How do I make an HTTP request and then wait for a response in C#

I am trying to talk with a Service that responds to SOAP requests. I have the call and response functionality working. The issue I face is that I want to make a request that can take some time to respond with the requested data. The Service will output the following SOAP message while the response is being prepared: "ProgressInfo Percent 0 /Percent /ProgressInfo"

The ProgressInfo messages will repeat until the actual data is finally sent.

I wish to Send the message through an HttpWebRequest, and then sit on the connection, processing the ProgressInfo messages until the real message comes through.

This is what I have so far:

    public string Send_SOAP_Request_And_Recieve_Response( string iInterface, string iRequestMessage )
    {
        bool  receivedFinalMessage = false;
        string oResponseText = "";
        HttpWebRequest request = Create_Message( MESSAGE_TYPE_POST, iInterface );
        try
        {
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes( iRequestMessage );
            request.ContentLength = bytes.Length;

            using (Stream requestStream = request.GetRequestStream() )
            {
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ( response.StatusCode == HttpStatusCode.OK )
            {
                while (!receivedFinalMessage)
                {
                    using (Stream responseStream = response.GetResponseStream() )
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            oResponseText = reader.ReadToEnd();
                            int locationOfProgress = oResponseText.IndexOf( "ProgressInfo" );
                            if ( oResponseText.IndexOf( "ProgressInfo" ) == -1 && oResponseText.Length > 0 )
                            {
                                receivedFinalMessage = true;
                            }
                            else
                            {
                                // ?????
                                response = (HttpWebResponse)request.GetResponse();
                            }    
                        }
                    }
                }
            }
        }

The error message I get back the 2nd pass through is "The stream is not readable."

Any assistance would be greatly appreciated.

You will often get this error if you try to read a stream's contents more than once, including in the debugger. Simplify your request with the following code, and then look at the string response:

string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
    {
        soapResult = rd.ReadToEnd();
    }
    Console.Write(soapResult);        
}

Don't put rd in your watch list, rather, look at the value of soapResult.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM