C# HttpWebRequest problems

MGMorden

Diamond Member
Jul 4, 2000
3,348
0
76
Hey guys. I'm running into an issue here and figured I'd ask for a bit of advice. I've got an app that needs to interface with a third party utility. Specifically, it needs to submit an address to the utility and have it process it for CASS certification.

The only "server mode" that this utility allows though, is it has a web interface that allows you to type in the criteria and submit them in. It can then respond back with either an HTML or XML file (your choice) which you can then parse.

So, my plan was simple: have my C# program simply hit the HTML page, get the XML response back, and then parse my data out of that.

The following function is one that I borrowed and modified a bit from a quick web example on doing this.

Code:
        private string GetResponseWithPost(string StrURL, string strPostData)
        {
            string strReturn = "";
            HttpWebRequest objRequest = null;
            ASCIIEncoding objEncoding = new ASCIIEncoding();
            Stream reqStream = null;
            HttpWebResponse objResponse = null;
            StreamReader objReader = null;
            try
            {
                objRequest = (HttpWebRequest)WebRequest.Create(StrURL);

                objRequest.Method = "POST";
                byte[] objBytes = objEncoding.GetBytes(strPostData);
                objRequest.ContentLength = objBytes.Length;
                objRequest.ContentType = "application/x-www-form-urlencoded";
                reqStream = objRequest.GetRequestStream();
                reqStream.Write(objBytes, 0, objBytes.Length);

                objResponse = (HttpWebResponse)objRequest.GetResponse();
                objReader = new StreamReader(objResponse.GetResponseStream());
                strReturn = objReader.ReadToEnd();

            }
            catch (Exception exp)
            {
                throw exp;
            }
            finally
            {
                objRequest = null;
                objEncoding = null;
                reqStream = null;
                if (objResponse != null)
                    objResponse.Close();
                objResponse = null;
                objReader = null;
            }
            return strReturn;
        }

Using this, the program works just fine. Here is the code used to actually create the call the function and put the XML into a string. As you can tell, the original address that I'm sending is contained in a series of text boxes.

Code:
string response;
string postData = "company=" + textBoxOrigAddress1.Text + "&address=" + textBoxOrigAddress1.Text + "&address2=" + textBoxOrigAddress2.Text + "&city=" + textBoxOrigCity.Text + "&state=" + comboBoxOrigState.Text + "&zip=" + textBoxOrigZip.Text;

All comes back great. The problem however, is the speed. It's highly variable - taking anywhere from 5 to 15 seconds to get a response back from the web server. This really, really adds up when doing submissions. Thing is, submitting the web form from a browser results in a nearly instant response, leading me to believe that there is some issue there.

Any ideas on this? Any obvious reason why the function above would behave so slowly?

Thanks.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I don't see any obvious reason why it should take longer. Try using a proxy like Fiddler to capture the requests in both cases and compare them.
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
The web server is probably slow. Can you test a request in a browser?

Maybe try closing the request stream.

Personally I'd use the WebClient class instead but I doubt that is your problem.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Download Wireshark (it's free) and compare what your app sends and receives against the traffic using IE.

I'd suggest IE over Firefox since then you're using MS internet libraries for both tests.

Some possible differences:
- Errors or missing variables in your POST data
- Missing cookies (could be set by JavaScript in web pages rather than passed as SetCookie headers)
- Missing headers (sometimes the server might care about the Referer: header)
- User-Agent string
 
Last edited:

Oyster

Member
Nov 20, 2008
151
0
0
Have you tried "chunking" your streams?

http://stackoverflow.com/questions/16998/reading-chunked-response-with-httpwebresponse

Depending on the load on your server, it is possible that the application is unable to cache the data in the RAM, going straight to virtual memory and slowing things down. Notice that the link uses an 8 meg chunk - that's pretty big, so I would reduce it to something like 2 megs.

If this seems to improve performance, I'd probably implement the same solution for your HttpWebRequest portion, too.
 

MGMorden

Diamond Member
Jul 4, 2000
3,348
0
76
Update: Thanks for the response guys. I managed to track down the issue. Due to the way the WebRequest object was being created, it was trying to perform automatic proxy detection each time the object was created, which was slowing down the app. I disabled auto-proxy detection in the application's config file and it is now working MUCH better.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Update: Thanks for the response guys. I managed to track down the issue. Due to the way the WebRequest object was being created, it was trying to perform automatic proxy detection each time the object was created, which was slowing down the app. I disabled auto-proxy detection in the application's config file and it is now working MUCH better.

Thanks for updating this thread with the solution.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |