1

I'm talking about POST requests, using:

WebClient wc = new WebClient(); String result = wc.UploadString("http://example.com/", "data=hello, world!"); 

Edit: This is my actual code right now:

String result; using (WebClient wc = new WebClient()) { result = wc.UploadString("http://" + "pastebin.com/api_public.php", "POST", "paste_code=" + LongDataEscape(Clipboard.GetText())); } 

And if you're wondering about LongDataEscape:

 public String LongDataEscape(String Str) { String Output = ""; int ByteCount = 32766; if (Str.Length > ByteCount) { for (int i = 0; i < Str.Length; i+= ByteCount) { if (Str.Length - i < ByteCount) Output += Uri.EscapeDataString(Str.Substring(i, Str.Length - i)); else Output += Uri.EscapeDataString(Str.Substring(i, ByteCount)); } } else Output = Uri.EscapeDataString(Str); return Output; } 

The first time I execute the above piece of code it always takes around 15 seconds (ok maybe 10), no matter what website it is to, but the same code pieces that follow are just instantly.

I was thinking there might be some setting that does it, but I haven't found out yet.

2
  • It sounds like it's waiting for a timeout. Is it possible that you have a proxy setting it's trying to use? I'd recommend using Process Monitor (technet.microsoft.com/en-us/sysinternals/bb896645.aspx) to see what's going on during the 10-15 seconds. Commented Nov 7, 2010 at 4:19
  • @Gabe I'm not doing such a thing. I just tried procmon and it didn't show anything interesting in the 10 second interval. Just that didn't do anything... Commented Nov 7, 2010 at 16:29

2 Answers 2

4

I fixed it.

When you make a new WebClient object (FtpWebRequest too) you have to set the "Proxy" property of it to null. For example:

WebClient wc = new WebClient(); wc.Proxy = null; 

Then the first request will never take long and you'll have no problems.

Sign up to request clarification or add additional context in comments.

1 Comment

Good you found an solution, but I suggest you change the proxy settings instead, this way you'll ignore your users preferences by never using a proxy, even if they have specified one.
0

The first call often take longer than subsequent ones, however 15s is too much.

Try to make the following change:

using(WebClient wc = new WebClient()) { String result = wc.UploadString("http://example.com/", "data=hello, world!"); } 

8 Comments

Yeah, maybe I am thinking too much with 15 seconds, but there isn't a way this initial call time can be reduced?
@Angelo Geels, have you tried with my proposed change? I suspect the 15s delay is due to you not closing the WebClient correctly.
@Angelo Geels, can you update your post with the exact code you are running? I mean you aren't calling example.com right?
@Angel Geels, good :). Can you verify with fiddler that it is indeed the HTTP request that takes a long time.
@Angelo Geels, you can get Fiddler from www.fiddler2.com
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.