C#发布,Webrequest循环不起作用,为i = 2循环,则无结果

时间:2018-12-16 10:24:01

标签: c# stream http-post httpwebrequest httpwebresponse

如何将以下代码修改为循环,我尝试了不带循环的代码,并且可以正常工作,所以我在2次迭代后添加了循环,但没有结果也没有错误 我尝试了Google提供的许多解决方案,但没有任何进展,也许是因为我是初学者

            Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
        session_Data[2] = unixTimestamp.ToString();

        try
        {
            string url = "";
            HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url);
            IWebProxy proxy = request1.Proxy;
            if (proxy != null)
            {
                Console.WriteLine("Proxy: {0}", proxy.GetProxy(request1.RequestUri));
            }
            else
            {
                Console.WriteLine("Proxy is null; no proxy will be used");
            }

            WebProxy myProxy = new WebProxy();
            Uri newUri = new Uri("http://" + ip + ":" + port);
            // Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
            myProxy.Address = newUri;
            // Create a NetworkCredential object and associate it with the 
            // Proxy property of request object.
            myProxy.Credentials = new NetworkCredential(user, pass);
            request1.Proxy = myProxy;

            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "{\"device\"}";
            byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(postData);


            WebRequest request = request1;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            Stream stream = request.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();

            WebResponse response = request.GetResponse();
            stream = response.GetResponseStream();

            StreamReader sr = new StreamReader(stream);

            Console.WriteLine(sr.ReadToEnd());

            JObject session = JObject.Parse(sr.ReadToEnd());
            sr.Close();
            stream.Close();


        }
        catch (Exception ex)
        {
            this.Error = ex.Message;
        }

1 个答案:

答案 0 :(得分:0)

在方法内部实现此功能并使用异步,您可以等待每次响应执行。提供回调,它将处理每个请求的完成,您可以在循环中使用它:

 public async Task Process(Action ProcessFinished)
 {
    Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
    session_Data[2] = unixTimestamp.ToString();

    try
    {
        string url = "";
        HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url);
        IWebProxy proxy = request1.Proxy;
        if (proxy != null)
        {
            Console.WriteLine("Proxy: {0}", proxy.GetProxy(request1.RequestUri));
        }
        else
        {
            Console.WriteLine("Proxy is null; no proxy will be used");
        }

        WebProxy myProxy = new WebProxy();
        Uri newUri = new Uri("http://" + ip + ":" + port);
        // Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
        myProxy.Address = newUri;
        // Create a NetworkCredential object and associate it with the 
        // Proxy property of request object.
        myProxy.Credentials = new NetworkCredential(user, pass);
        request1.Proxy = myProxy;

        ASCIIEncoding encoding = new ASCIIEncoding();
        string postData = "{\"device\"}";
        byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(postData);


        WebRequest request = request1;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        Stream stream = request.GetRequestStream();
        stream.Write(data, 0, data.Length);
        stream.Close();

        WebResponse response = request.GetResponse();
        stream = response.GetResponseStream();

        StreamReader sr = new StreamReader(stream);

        Console.WriteLine(sr.ReadToEnd());

        JObject session = JObject.Parse(sr.ReadToEnd());
        sr.Close();
        stream.Close();
    }
    catch (Exception ex)
    {
        this.Error = ex.Message;
    }
}

public static void ProcessFinished()
{
   //Do some work after each request
}

for(var i=0; i<5; i++)
{
   await Process(ProcessFinished);
}