亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

c#許多異步HttpWebRequest

c#許多異步HttpWebRequest

C#
瀟瀟雨雨 2022-01-16 15:37:58
我嘗試制作許多 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 begin1 begin2 begin3 begin4 begin5 begin6 begin7 begin8 begin9 begin0 status code: OK0 end1 status code: OK1 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).但是沒有任何例外,并且控制臺沒有關閉。我的錯誤在哪里?
查看完整描述

3 回答

?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

不記得 response.Dispose();


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");

    response.Dispose();

}


查看完整回答
反對 回復 2022-01-16
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

我已運行您的代碼并獲得如下輸出:


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

5 status code: OK

5 end

8 status code: OK

8 end

4 status code: OK

4 end

3 status code: OK

3 end

2 status code: OK

2 end

6 status code: OK

6 end

1 status code: OK

1 end

7 status code: OK

7 end

9 status code: OK

9 end

當你按下回車鍵時,應用程序將因為Console.ReadLine();你的 main 方法而關閉。它等待程序直到從您的控制臺獲得輸入。


查看完整回答
反對 回復 2022-01-16
?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

我會把它變成它自己的靜態方法,它會返回一個字符串,這樣你就可以看到發生了什么。我不會依賴任何類型的 xml 文件,除非它是從 API 調用返回的 XML。我在這里發帖,但如果你愿意,你可以得到。不確定您的設置在后端如何。核實:


public static string PostXMLDataCS()

    {

        bool debugging = false;

        try

        {


            string iConnectAuth = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +

  "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">" +

            "<soapenv:Header/>" +

       "<soapenv:Body>" +

        "<tem:Authenticate>" +

         "<!--Optional:-->" +

          "<tem:TenantID>TenantID</tem:TenantID>" +

               "<!--Optional:-->" +

                "<tem:Username>Username</tem:Username>" +

                     "<!--Optional:-->" +

                      "<tem:Password>password</tem:Password>" +

                           "</tem:Authenticate>" +

                            "</soapenv:Body>" +

                             "</soapenv:Envelope>";


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.example.com/services/ByDesign/Inventory.svc");

            byte[] bytes;

            bytes = System.Text.Encoding.ASCII.GetBytes(iConnectAuth);


            request.ContentType = "text/xml; charset=utf-8";

            request.Accept = "gzip,deflate";

            request.ContentLength = bytes.Length;

            request.Method = "POST";

            request.Headers.Add("SOAPAction", "http://tempuri.org/IInventory/Authenticate");

            request.KeepAlive = true;


            Stream requestStream = request.GetRequestStream();

            requestStream.Write(bytes, 0, bytes.Length);

            requestStream.Close();

            HttpWebResponse response;

            response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)

            {

                Stream responseStream = response.GetResponseStream();

                string responseStr = new StreamReader(responseStream).ReadToEnd();

                response.Close();

                //MessageBox.Show(responseStr);

                return responseStr;

            }

        }

        catch (Exception e)

        {

            if (debugging == true)

            {

                MessageBox.Show("There was a problem authenticating for the check inventory with iConnect. Error: " + e);

            }


            string messageSubject = "There was a problem authenticating for the check inventory with iConnect.";

            string messageBody = "There was a problem authenticating for the check inventory with iConnect. Error: ";

            string kiboSendEmail = string.Empty;

            SendEmail sendEmail = new SendEmail();

            return kiboSendEmail = sendEmail.SendEmailCS(messageSubject, messageBody, e);


        }

        return null;

    }


查看完整回答
反對 回復 2022-01-16
  • 3 回答
  • 0 關注
  • 588 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號