1

So I need to get two responses from a request. The documentation from de webservice I need to call states that I will recieve a quick synchonous response and after that it will send me a slow second async response.

To me this seems very weird and I can't get .NET to wait/listenen/poll for the second response.

What I got is the following;

public void testCall(object sender, EventArgs e)
{
    byte[] xmlIn = Encoding.UTF8.GetBytes("my xml");

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("my url");
    request.Method = "POST";
    request.ContentType = "application/xml";

    using (var dataStream = request.GetRequestStream())
    {
        dataStream.Write(xmlIn, 0, xmlIn.Length);
    }

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

private void FinishWebRequest(IAsyncResult result)
{       
    HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;

    using (var stream = response.GetResponseStream())
    {
        StreamReader reader = new StreamReader(stream, Encoding.UTF8);
        var responseString = reader.ReadToEnd();

        Debug.Write(responseString);
        Response.Write(responseString);
    }
}

Now this works for the first response.. but how do I get the second response?

Predders
  • 375
  • 3
  • 12
  • That would be the weirdest WebService I've ever seen. Can you say which one it is? Can you cite the docs where this is stated? I could imagine, the quick sync responds contains a resource to poll for the calculation result, though. But if it is really as you write, that would be ... I don't know. "Let's reinvent the wheel" – Fildor Aug 26 '19 at 15:22
  • @Fildor Yes indeed, this is very weird! And also the webservice only has one method to call. The first result does house an id, but there is no method to call with that id, to poll or something. – Predders Aug 27 '19 at 06:46
  • Again, is it a public webservice? Can you please tell us, what it is? I cannot wrap my head around somebody really implemented it like this. That would mean you cannot use any standard implementations. You'd have to go down to TCP level and parse everything yourself. All standard HTTP stuff that I am aware of will close the connection after the response is received. That's how it works. Open, request, wait for response, receive or timeout, close. – Fildor Aug 27 '19 at 08:05
  • @Fildor sadly it is not a public webservice. I have the documentation, but I don't know how to share this here. – Predders Aug 27 '19 at 08:11

1 Answers1

0

you should know, I dont think http is designed for this use case. Maybe you can consider System.Net.Sockets. But it's worth a try using StreamReader without disposing it (get rid of your using statement) and manually closing it when you want to by breaking out of an infinite loop and calling StreamReader.Close().

And if you go the infinite loop route, consider a timeout threshold and using s separate thread.

terrencep
  • 675
  • 5
  • 16