Question

Slow initial response for .NET 2.0 HttpWebRequest.GetResponse();

Asked by: NZulli

I have a simple piece of code that runs on Windows Mobile 5.0 handheld device that opens up an HTTP request and reads the response.  (Code below...)

Trouble is, the first time this is executed, it takes upwards of 30 seconds to reply.  Subsequent requests are fast, almost immediate.  The delay happens when calling GetResponse() (line 2 below...).

I have done extensive searching on the issue, and many people believe this is due to a proxy lookup.  I have tried setting httpRequest.Proxy = null; which gives an error.  I also tried:
httpRequest.Proxy = GlobalProxySelection.GetEmptyWebProxy();, which does not give an error but initial connections are still slow.

I ran a test to see if my url is bypassed by the proxy too, and it is. -
MessageBox.Show(httpRequest.Proxy.IsBypassed(new Uri(url))); popped up "true"

Another suggestion I found is to add proxy bypass in the app.config file, but the Windows Moble compact framework does not support .config files so I cannot do this.

I also did a packet scan on both our main proxy server (which it shouldn't be accessing) and the web server that the url is pointing to.  The proxy came up empty, and the web server only showed traffic after the delay.  This means to me that the device is doing some kind of internal process during the 30 second delay.


Does anyone have other suggestions as to how to fix this?  This problem is a showstopper, and the project will be discarded if this is not resolved.

Thanks!!!

HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

                                  
1:
2:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-09-09 at 14:11:06ID24719609
Tags

.NET 2.0

,

HttpWebRequest

,

HttpWebResponse

,

slow first connection

Topics

.Net Compact Framework

,

Web Languages/Standards

,

Windows Mobile (Pocket PC / Win CE) Operating Systems

Participating Experts
1
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Compacting & Replication
    I'm close to finishing a simple application that will be used 95% for mobile Salespeople. There will be 3 different servers, and my network folks informed me that they will use Active Directory to push updates within the topology (does this make sense to you?). Access is no...
  2. Handheld newbie question
    I'm interested in buying a handheld PC, but I also like the HD in iPods ... is there a good compromise that gives you programmability/win ce/lcd display and storage in the 20G/40G vicinity?
  3. Open database is slow after compacting
    To whom it may concern, I have scheduled the compacting databases at server using this command Compact <database> -i -c -d -K Updall -R After compacting, when users open the databases at their PC first time, it was very very slow, but subsequent access is normal. It...
  4. Designing an Application for a Handheld Device.
    Hi, I've been asked to develop a simple VB.NET 2005 application for a handheld Palm device, but I've never attempted to do such a thing. Is it very different than developing applications for regular PCs? Would it matter if the device was a Palm or a Windows O/S based device?...
  5. Need to find a small web server that can run on a windows…
    I need to find a web server that will run on a Windows Mobile handheld. I need to create a web based front end with html pages and have them connect to a local sql compact database on the handheld. Anyone know of any software that will do this or a way to set up the Windows M...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: alexey_gusevPosted on 2009-09-09 at 21:45:29ID: 25297449

try using IP instead of URL maybe, and see if it makes any difference?

 

by: NZulliPosted on 2009-09-10 at 07:15:04ID: 25300423

Good idea, or so I thought.

I put in the IP, which gave me a 403 Forbidden error.  However, the delay still showed up before I got the 403 error.  This would seem to mean it doesn't even make the request to the page until something internal happens.

 

by: alexey_gusevPosted on 2009-09-10 at 07:16:53ID: 25300440

have you tried to check different versions and service packs of cf ?

 

by: NZulliPosted on 2009-09-10 at 07:23:20ID: 25300515

I just ran cgacutil.exe.

Compact Framework 2.0.7045.0, 1.0.4292.2

I believe this corresponds to 2.0 SP2 and 1.0 SP3 (not sure what that trailing .2 means)

 

by: alexey_gusevPosted on 2009-09-10 at 07:25:50ID: 25300540

hmm, ok. could you try Win32 API instead of using cf.net?
I don't know the immediate reason for such behaviour, but it might be something in cf.net

 

by: NZulliPosted on 2009-09-10 at 08:03:34ID: 25300936

I remember reading somewhere that this was specifically a .NET v2 issue that wasn't in .NET v1.  It was referring to the proxy lookup though, which their workaround did not work for me.

While I'm looking into the possibility (i.e. syntax) of using the Win32 API, do you know if I would have to rewrite the whole project to accomplish this or can I just specify the references and only do the HTTP lookups using Win32?

Also, if it is true that it's a v2 issue, can I perform the lookups using v1 code?  I don't know if you can mix versions that way...

 

by: alexey_gusevPosted on 2009-09-10 at 08:08:59ID: 25301000

I'd suggest to go with P/Invoke for Win32 calls, you shouldn't need many structs there, so it should be more or less straightforward.

So what you would need to do is that:

- declare P/Invoke methods for WinInet calls
- use them instead of HttpRequest etc

At least, this way you can check that it's cf.net problem

 

by: NZulliPosted on 2009-09-10 at 13:30:55ID: 25303954

OK.  I just spent several hours working on this (good learning experience!).  I believe we have the same problem though.

Code below is what I did.  Do you see any obvious syntax errors or something I messed up?

Assuming I did it right, the delay still occurs during the InternetOpenUrlW(<...>) call.

How frustrating...

[DllImport("wininet.dll")]
static extern IntPtr InternetOpenW(
     string lpszAgent, int dwAccessType, string lpszProxyName, 
     string lpszProxyBypass, int dwFlags);
[DllImport("wininet.dll")]
static extern IntPtr InternetConnectW(
     IntPtr hInternet, string lpszServerName, short nServerPort,
     string lpszUsername, string lpszPassword, int dwService,
     int dwFlags, int dwContext);
[DllImport("wininet.dll")]
static extern IntPtr InternetOpenUrlW(
     IntPtr hInternet, string lpszUrl, string lpszHeaders,
     int dwHeadersLength, int dwFlags, IntPtr dwContext);
 
IntPtr hOpen = InternetOpenW(this.Name, 1, null, null, 0);
IntPtr hConnect = InternetConnectW(hOpen, clsGlobal.ServerURL, 80, 
     null, null, 0, 0, 0);
IntPtr hFile = InternetOpenUrlW(hOpen, url, null, 0, 0, IntPtr.Zero);

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:

Select allOpen in new window

 

by: alexey_gusevPosted on 2009-09-10 at 14:13:03ID: 25304429

well, InternetOpenUrl may be a problem, because it does many things under the hood. Try different approach (eg my own old article here, look for C++ part : )  - http://www.developer.com/net/cplus/article.php/10919_3429261_2 )

this doesn't guarantee it will make any difference, but it is definitely something to do with connection establishing or DNS lookup.

 

by: NZulliPosted on 2009-09-10 at 15:07:33ID: 25305022

Thanks for the article, it was very helpful.

After working out the details of getting that method in place, it looks like the problem still occurs.  Code below.

Delay still occurs during the HttpSendRequestW(<...>) call.

[DllImport("wininet.dll",SetLastError=true)]
static extern IntPtr InternetOpenW(
   string lpszAgent, int dwAccessType, string lpszProxyName, 
   string lpszProxyBypass, int dwFlags);
[DllImport("wininet.dll", SetLastError = true)]
static extern IntPtr InternetConnectW(
   IntPtr hInternet, string lpszServerName, short nServerPort,
   string lpszUsername, string lpszPassword, int dwService,
   int dwFlags, int dwContext);
[DllImport("wininet.dll", SetLastError = true)]
static extern IntPtr HttpOpenRequestW(
   IntPtr hHttpSession, string lpszVerb, string lpszObjectName,
   string lpszVersion, string lpszReferer, string[] lplpszAcceptTypes,
   int dwFlags, IntPtr dwContext);
[DllImport("wininet.dll", SetLastError = true)]
static extern bool HttpSendRequestW(IntPtr hRequest, string lpszHeaders, 
   int dwHeadersLength, string lpOptional, int dwOptionalLength);
 
IntPtr hOpen = InternetOpenW(this.Name, 1, null, null, 0);
IntPtr hConnect = InternetConnectW(hOpen, "<servername>", 80, null, null, 3, 0, 0);
IntPtr hRequest = HttpOpenRequestW(hConnect, "GET", "<virtual directory>/<page>",
   null, null, st, 0, IntPtr.Zero);
bool hSendRequest = HttpSendRequestW(hRequest, null, 0, null, 0);
 
if (hSendRequest == false)
{
   MessageBox.Show(Marshal.GetLastWin32Error().ToString());
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:

Select allOpen in new window

 

by: NZulliPosted on 2009-09-10 at 15:14:04ID: 25305062

Perhaps I spoke too soon.  I put the IP address in there as you first suggested and it seemed to go right through.  Let me finish with the InternetReadFile() implementation and see how that part goes before we decide to look elsewhere.

Thanks for your help so far!!!

 

by: NZulliPosted on 2009-09-11 at 11:08:14ID: 25312005

Well it looks like this is a solution!

For posterity sake, I will include the C# code I added to get this working (ported from C++ using MSDN and the article referenced above).

Another thing I found out is that WinHTTP seems to be an updated version of WinINET, but Windows Mobile 5 only has WinINET in the \Windows folder.  That is what I used, and it is working.

The delay had to have been something with a DNS/WINS lookup or another network issue, though I was never able to track it down.  WinINET allows the connection to the server and passes the rest of the URL path.  After implementation, the final resolution was to put the IP address in as the servername.

Thank you very much alexey_gusev for your assistance in resolving this issue!

using System.Runtime.InteropServices;
 
// THIS GOES IN THE GLOBAL DECLARATION SECTION
        // Use Win32 API to perform INET calls
        // These declarations allow use of the imported DLL methods
        // (Declarations translated from C++ code on MSDN website)
[DllImport("wininet.dll", SetLastError = true)]
static extern IntPtr InternetOpenW(
   string lpszAgent, int dwAccessType, string lpszProxyName,
   string lpszProxyBypass, int dwFlags);
[DllImport("wininet.dll", SetLastError = true)]
static extern IntPtr InternetConnectW(
   IntPtr hInternet, string lpszServerName, short nServerPort,
   string lpszUsername, string lpszPassword, int dwService,
   int dwFlags, int dwContext);
[DllImport("wininet.dll", SetLastError = true)]
static extern IntPtr HttpOpenRequestW(
   IntPtr hHttpSession, string lpszVerb, string lpszObjectName,
   string lpszVersion, string lpszReferer, string[] lplpszAcceptTypes,
   int dwFlags, IntPtr dwContext);
[DllImport("wininet.dll", SetLastError = true)]
static extern bool HttpSendRequestW(IntPtr hRequest, string lpszHeaders,
   int dwHeadersLength, string lpOptional, int dwOptionalLength);
[DllImport("wininet.dll", SetLastError = true)]
static extern bool InternetReadFile(
   IntPtr hFile, byte[] lpBuffer, int dwNumberOfBytesToRead,
   ref int lpdwNumberOfBytesRead);
[DllImport("wininet.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool InternetCloseHandle(IntPtr hInternet);
 
 
// THIS GOES IN YOUR CODE
string servername = "<server>";
string relativePath = "<virtual directory>/<path>";
        // Perform HTTP lookup using Win32 API
string[] st = new string[] { };
        // 1 means direct connection, no proxy
        // null,null is the username/password for the proxy
IntPtr hOpen = InternetOpenW(this.Name, 1, null, null, 0);
IntPtr hConnect = InternetConnectW(hOpen, servername, 80, null, null, 3, 0, 0);
IntPtr hRequest = HttpOpenRequestW(hConnect, "GET", relativePath, null, null, st, 
   0, IntPtr.Zero);
bool hSendRequest = HttpSendRequestW(hRequest, null, 0, null, 0);
if (hSendRequest == false)
{
        // Some kind of server error, displayed as error code
   throw new ApplicationException("Win32 Error: " + 
      Marshal.GetLastWin32Error().ToString());
}
 
int lengthOfResponse = 4096;
byte[] buffer = new byte[lengthOfResponse];
int dwRead = 0;
string response = "";
bool tmp;
for (int i = 0; i < lengthOfResponse; i++)
{
   tmp = InternetReadFile(hRequest, buffer, lengthOfResponse, ref dwRead);
   if (tmp == false)
   {
      break;
   }
   response += Encoding.ASCII.GetString(buffer, 0, dwRead);
}
 
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hOpen);
 
// OUTPUT IS IN VARIABLE response
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:

Select allOpen in new window

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...