【发布时间】:2010-11-06 21:14:17
【问题描述】:
我说的是 POST 请求,使用:
WebClient wc = new WebClient();
String result = wc.UploadString("http://example.com/", "data=hello, world!");
编辑:这是我现在的实际代码:
String result;
using (WebClient wc = new WebClient())
{
result = wc.UploadString("http://" + "pastebin.com/api_public.php", "POST", "paste_code=" + LongDataEscape(Clipboard.GetText()));
}
如果您想了解 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;
}
我第一次执行上面的代码总是需要大约 15 秒(好吧也许是 10 秒),不管它是在哪个网站,但随后的相同代码片段只是瞬间完成。
我在想可能有一些设置可以做到这一点,但我还没有发现。
【问题讨论】:
-
听起来像是在等待超时。您是否有可能尝试使用代理设置?我建议使用 Process Monitor (technet.microsoft.com/en-us/sysinternals/bb896645.aspx) 来查看 10-15 秒内发生了什么。
-
@Gabe 我没有这样做。我刚刚尝试了 procmon,但它在 10 秒的间隔内没有显示任何有趣的东西。只是没有做任何事情......
标签: c# .net performance webclient