如何提高HttpWebRequest并发效率

kitten_scratch 2008-10-16 12:34:19
我通过HttpWebRequest发起一个GET的Request,不需要等待服务器返回结果,只要发出请求即可。
请问我该怎么做?
...全文
767 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
diffmaker 2008-10-16
  • 打赏
  • 举报
回复
用异步请求
lovehongyun 2008-10-16
  • 打赏
  • 举报
回复

string uri = " http://server/path/WebForm.aspx";

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);

req.Method = "GET";

req.MaximumAutomaticRedirections = 3;

req.Timeout = 5000;

Console.WriteLine("Sending HTTP request");

HttpWebResponse res = (HttpWebResponse)req.GetResponse();

Stream resst = res.GetResponseStream();

StreamReader sr = new StreamReader(resst);

Console.WriteLine("HTTP Response is: ");

Console.WriteLine(sr.ReadToEnd());

sr.Close();

resst.Close();
jinjazz 2008-10-16
  • 打赏
  • 举报
回复
用WebClient
lovehongyun 2008-10-16
  • 打赏
  • 举报
回复

string strURL = "http://localhost/xxxx/xxxx.aspx?keyword=";
strURL +=this.textBox1.Text;
System.Net.HttpWebRequest request;
//创建一个HTTP请求
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
//request.Method="get";
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
XmlTextReader Reader = new XmlTextReader(s);
Reader.MoveToContent();
string strValue = Reader.ReadInnerXml();
strValue = strValue.Replace("<","<");
strValue = strValue.Replace(">",">");
MessageBox.Show(strValue);
Reader.Close();

888228 2008-10-16
  • 打赏
  • 举报
回复
2楼想告诉你的应该是开多线程
kitten_scratch 2008-10-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 Gregorian 的回复:]
public static void SendHttpRequest()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

Console.WriteLine("Main thread does some work, then sleeps.");
// If you comment out the Sleep, the main thread exits before
// the thread pool task runs. The thread pool uses background
// threads, which do not keep…
[/Quote]

如果我要多个并发,那么如何调用?
谢谢...
kitten_scratch 2008-10-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 diffmaker 的回复:]
用异步请求
[/Quote]

从网上down国一个支持异步的类,测试了一下,每秒大概是7~8个请求发出,现在我需要每秒大概20个。
Gregorian 2008-10-16
  • 打赏
  • 举报
回复
public static void SendHttpRequest()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

Console.WriteLine("Main thread does some work, then sleeps.");
// If you comment out the Sleep, the main thread exits before
// the thread pool task runs. The thread pool uses background
// threads, which do not keep the application running. (This
// is a simple example of a race condition.)
Thread.Sleep(10000);

Console.WriteLine("Main thread exits.");
}

// This thread procedure performs the task.
static void ThreadProc(Object stateInfo)
{
// send the http request
HttpWebRequest request = HttpWebRequest.CreateDefault(new Uri("http://www.sina.com.cn")) as HttpWebRequest;
WebResponse response = request.GetResponse();
}

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧