C# 在没有响应的情况下发送Web请求

C# 在没有响应的情况下发送Web请求,c#,httpwebrequest,C#,Httpwebrequest,我在这个控制台程序中遇到了一些问题。我想用这个循环向网站发送许多请求,但没有响应,因为响应会增加更多延迟,我不需要响应 所以在我运行这个程序后,它只发送两个请求,然后它停止,什么也不做。请帮我解决这个问题 namespace ConsoleApplication8 { class Program { static void Main(string[] args) { string post2; for

我在这个控制台程序中遇到了一些问题。我想用这个循环向网站发送许多请求,但没有响应,因为响应会增加更多延迟,我不需要响应

所以在我运行这个程序后,它只发送两个请求,然后它停止,什么也不做。请帮我解决这个问题

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            string post2;
            for (int i = 111; i < 222; i++)
            {
                post2 = i.ToString();
                System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://google.com");
                request.Method = "POST";
                request.KeepAlive = true;
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.ServicePoint.Expect100Continue = false;
                string postData = post2;
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentLength = byteArray.Length;
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);         
                Console.WriteLine(post2);
            }                              
        }
    }
}
命名空间控制台应用程序8
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串post2;
对于(int i=111;i<222;i++)
{
post2=i.ToString();
System.Net.HttpWebRequest request=(HttpWebRequest)WebRequest.Create(“http://google.com");
request.Method=“POST”;
request.KeepAlive=true;
request.UserAgent=“Mozilla/5.0(Windows NT 5.1;rv:19.0)Gecko/20100101 Firefox/19.0”;
request.ContentType=“application/x-www-form-urlencoded”;
request.Accept=“text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”;
request.ServicePoint.Expect100Continue=false;
字符串postData=post2;
byte[]byteArray=Encoding.UTF8.GetBytes(postData);
request.ContentLength=byteArray.Length;
Stream dataStream=request.GetRequestStream();
写入(byteArray,0,byteArray.Length);
控制台写入线(post2);
}                              
}
}
}

.NET默认情况下限制为两个连接。如果需要,您可以增加限制,但真正的问题是您没有关闭流

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            string post2;
            for (int i = 111; i < 222; i++)
            {
                post2 = i.ToString();
                System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://google.com");
                request.Method = "POST";
                request.KeepAlive = true;
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.ServicePoint.Expect100Continue = false;
                string postData = post2;
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentLength = byteArray.Length;
                // You need to close the request stream when you're done writing to it.
                using(Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);         
                    Console.WriteLine(post2);
                }
                // Even if you don't need the response, you still need to close it.
                request.GetResponse().Close();
            }                              
        }
    }
}
命名空间控制台应用程序8
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串post2;
对于(int i=111;i<222;i++)
{
post2=i.ToString();
System.Net.HttpWebRequest request=(HttpWebRequest)WebRequest.Create(“http://google.com");
request.Method=“POST”;
request.KeepAlive=true;
request.UserAgent=“Mozilla/5.0(Windows NT 5.1;rv:19.0)Gecko/20100101 Firefox/19.0”;
request.ContentType=“application/x-www-form-urlencoded”;
request.Accept=“text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”;
request.ServicePoint.Expect100Continue=false;
字符串postData=post2;
byte[]byteArray=Encoding.UTF8.GetBytes(postData);
request.ContentLength=byteArray.Length;
//在完成对请求流的写入后,需要关闭请求流。
使用(Stream dataStream=request.GetRequestStream())
{
写入(byteArray,0,byteArray.Length);
控制台写入线(post2);
}
//即使你不需要回复,你仍然需要关闭它。
request.GetResponse().Close();
}                              
}
}
}

您是否收到错误消息?输出是什么?不,它是发送第一个和第二个请求,然后停止。当我在这个代码上按住鼠标指针时
Stream dataStream=request.GetRequestStream()向我显示此帮助:
Stream HttpWebRequest.GetRequestStream()(+1重载))
我认为如果将重载从一个数字更改为另一个数字可能会解决此问题,但我不知道如何编写此代码而不出错。您说:“.NET默认情况下限制为两个连接。您可以增加限制”正是我需要这个。请编写这些代码来更改默认连接。