c#许多异步HttpWebRequest

我尝试制作许多 async HttpWebRequest。这是我的测试代码:


class Program

{

    static void Main(string[] args)

    {

        Test();

        Console.ReadLine();

    }


    public static async void Test()

    {

        for (int i = 0; i < 10; i++)

        {

            int val = i;

            await Task.Run(() => WR(val));

        }

    }


    static async void WR(int msg)

    {

        Console.WriteLine(msg + " begin");


        string url = "https://stackoverflow.com";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.Method = "GET";

        var response = (HttpWebResponse)await Task.Factory.FromAsync<WebResponse>

                (request.BeginGetResponse, request.EndGetResponse, null);


        Console.WriteLine(msg + " status code: " + response.StatusCode);

        Console.WriteLine(msg + " end");

    }

}

结果:


0 begin

1 begin

2 begin

3 begin

4 begin

5 begin

6 begin

7 begin

8 begin

9 begin

0 status code: OK

0 end

1 status code: OK

1 end

然后1 end什么也没发生。在输出大约 30 秒后,我可以看到:


The thread 0x6634 has exited with code 0 (0x0).

The thread 0x5620 has exited with code 0 (0x0).

The thread 0x4d08 has exited with code 0 (0x0).

The thread 0x39b8 has exited with code 0 (0x0).

The thread 0x3454 has exited with code 0 (0x0).

The thread 0x99c has exited with code 0 (0x0).

The thread 0x6be0 has exited with code 0 (0x0).

但是没有任何例外,并且控制台没有关闭。


我的错误在哪里?


潇潇雨雨
浏览 530回答 3
3回答

喵喵时光机

不记得 response.Dispose();static async void WR(int msg){&nbsp; &nbsp; Console.WriteLine(msg + " begin");&nbsp; &nbsp; string url = "https://stackoverflow.com";&nbsp; &nbsp; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);&nbsp; &nbsp; request.Method = "GET";&nbsp; &nbsp; var response = (HttpWebResponse)await Task.Factory.FromAsync<WebResponse>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (request.BeginGetResponse, request.EndGetResponse, null);&nbsp; &nbsp; Console.WriteLine(msg + " status code: " + response.StatusCode);&nbsp; &nbsp; Console.WriteLine(msg + " end");&nbsp; &nbsp; response.Dispose();}

qq_花开花谢_0

我已运行您的代码并获得如下输出:0 begin1 begin2 begin3 begin4 begin5 begin6 begin7 begin8 begin9 begin0 status code: OK0 end5 status code: OK5 end8 status code: OK8 end4 status code: OK4 end3 status code: OK3 end2 status code: OK2 end6 status code: OK6 end1 status code: OK1 end7 status code: OK7 end9 status code: OK9 end当你按下回车键时,应用程序将因为Console.ReadLine();你的 main 方法而关闭。它等待程序直到从您的控制台获得输入。

动漫人物

我会把它变成它自己的静态方法,它会返回一个字符串,这样你就可以看到发生了什么。我不会依赖任何类型的 xml 文件,除非它是从 API 调用返回的 XML。我在这里发帖,但如果你愿意,你可以得到。不确定您的设置在后端如何。核实:public static string PostXMLDataCS()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; bool debugging = false;&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string iConnectAuth = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +&nbsp; "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<soapenv:Header/>" +&nbsp; &nbsp; &nbsp; &nbsp;"<soapenv:Body>" +&nbsp; &nbsp; &nbsp; &nbsp; "<tem:Authenticate>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"<!--Optional:-->" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<tem:TenantID>TenantID</tem:TenantID>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"<!--Optional:-->" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<tem:Username>Username</tem:Username>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"<!--Optional:-->" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<tem:Password>password</tem:Password>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"</tem:Authenticate>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</soapenv:Body>" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"</soapenv:Envelope>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.example.com/services/ByDesign/Inventory.svc");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] bytes;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bytes = System.Text.Encoding.ASCII.GetBytes(iConnectAuth);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.ContentType = "text/xml; charset=utf-8";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.Accept = "gzip,deflate";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.ContentLength = bytes.Length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.Method = "POST";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.Headers.Add("SOAPAction", "http://tempuri.org/IInventory/Authenticate");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.KeepAlive = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Stream requestStream = request.GetRequestStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; requestStream.Write(bytes, 0, bytes.Length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; requestStream.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpWebResponse response;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = (HttpWebResponse)request.GetResponse();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response.StatusCode == HttpStatusCode.OK)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Stream responseStream = response.GetResponseStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string responseStr = new StreamReader(responseStream).ReadToEnd();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //MessageBox.Show(responseStr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return responseStr;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (debugging == true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("There was a problem authenticating for the check inventory with iConnect. Error: " + e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string messageSubject = "There was a problem authenticating for the check inventory with iConnect.";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string messageBody = "There was a problem authenticating for the check inventory with iConnect. Error: ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string kiboSendEmail = string.Empty;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendEmail sendEmail = new SendEmail();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return kiboSendEmail = sendEmail.SendEmailCS(messageSubject, messageBody, e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP