开发者

Why is the first request I do with a WebClient object always taking 15 seconds?

开发者 https://www.devze.com 2023-01-23 15:57 出处:网络
I\'m talking about POST requests, using: WebClient wc = new WebClient(); String result = wc.UploadString(\"http://example.com/\", \"data=hello, world!\");

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开发者_如何学Go 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.


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.


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!");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消