0

I am trying to get response using below code

public void  Stream GetResponse()
{
    WebResponse response = webRequest.GetResponse();
    responseStream = response.GetResponseStream();
    return responseStream;
}

But it returns

500 - Internal server

error, so I am trying to GetResponse in using

public void  Stream GetResponse()
{
    using(WebResponse response = webRequest.GetResponse())
    {
         responseStream = response.GetResponseStream();
    }

    return responseStream;
}

Now when I try to access this responseStream from calling method it returns this error

The request was aborted: The connection was closed unexpectedly.

How do I get response in using and then return it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Richa
  • 3,261
  • 2
  • 27
  • 51
  • 1
    You can't. The `using` statement explicitly says to dispose the stream after leaving the using block. Anyone trying to use the stream you're returning will be working with a disposed stream. `500 - Internal server error` has nothing to do with your code. The server you're connecting to is throwing an error. – Rob Dec 11 '15 at 05:03
  • @Rob Thank you for explanation. But when more data is sent to request, it gets slow. So i was trying to follow http://stackoverflow.com/questions/2519655/httpwebrequest-is-extremely-slow – Richa Dec 11 '15 at 05:07
  • 1
    Whoever is calling `GetResponse` is responsible for disposing the stream. So you could do something like `using (var s = GetResponse()) { //do work }` – Rob Dec 11 '15 at 05:08
  • What does your data look like that you are requesting, and what does the rest of your code look like – Donald Jansen Dec 11 '15 at 05:22
  • @DonaldJansen I am basically sending data to SOAP. I have updated rest of code – Richa Dec 11 '15 at 05:24
  • have you tried to only post a xml body (and remove the SOAP Header), the url that you are sending to is it Asp.net ? – Donald Jansen Dec 11 '15 at 05:40
  • @Richa could you please confirm from your Server team, that SOAP is enabled on server. – Ram Singh Dec 11 '15 at 05:41
  • http://stackoverflow.com/questions/4791794/client-to-send-soap-request-and-received-response – Donald Jansen Dec 11 '15 at 05:44
  • @RamSingh Yes it was working fine till yesterday. Seems to be some issue with server i guess. – Richa Dec 11 '15 at 05:48

0 Answers0