HttpWebRequest to web-service in c#; how to get da

2020-06-30 04:22发布

问题:

I am calling a web-service with POST and receiving a 2MB xml. The problem is that it takes to much time until i can use the data within the Stream. The response seems to be after 7 secs there, but it takes another 10 sec to read the content(its a string) from response stream.

Stopwatch s = new Stopwatch();

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(MyUri);
req.Method = "POST";

req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = Poststring.Length;
s.Start();
StreamWriter swriter = new StreamWriter(req.GetRequestStream());
swriter.Write(Poststring);
swriter.Close();

// Get the response. 7 sec
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
s.Stop();
Debug.WriteLine("Talking to Web-Service: "+s.ElapsedMilliseconds);

s.Reset(); 
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);

// Read the content.  10 sec
XmlReader xmlReader = XmlReader.Create(dataStream);
s.Start();
XDocument xdoc = XDocument.Load(xmlReader);
s.Stop(); 
Debug.WriteLine("Convert stream to some useful data: "+s.ElapsedMilliseconds);

output in milliseconds

Talking to Web-Service: 6595
"Convert" stream to some useful data: 10772

Why does it take like 10 sec to read the content?? Is there stil some communication with the web-service or waiting for data when content is read? Its just a simple textfile (xml) with about 2MB. I thought that those 2 MB were transfered within the 6596 milliseconds. Because when i call that service with my browser, the xml content is shown in 6-7 sec.

The time for Talking to Web-Service is ok, but what is going on in those 10772 milliseconds?

Edit: The problem is stil there. I get different answers and they contradict each other.

回答1:

Add following

httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

resulting in:

Talking to Web-Service: 6595
"Convert" stream to some useful data: 256

Now i have the same performance like in a browser!



回答2:

XDocument xdoc = XDocument.Load(response.GetResponseStream(), LoadOptions.None);

Avoid the XmlReader.Create and use the XDocument.Load(Stream, LoadOptions) overload. http://msdn.microsoft.com/en-us/library/cc838321.aspx



回答3:

GetResponse will not return the full response stream. GetResponse will send your request and return an HttpWebResponse object based on the header information from the response. HttpWebResponse also has an associated stream from which you can read the full response body. This is precisely what you are doing.

I suspect that the 7-second delay you are seeing when you call GetResponse is a delay on the server in generating the XML document and sending a response. The further 10-second delay on XmlReader.Create is from reading the response stream (ie, downloading the file).

Is the XML generated dynamically? 7 seconds is not a terribly long time for an HTTP response, depending of course on your server location, quality, etc.