当前位置:   article > 正文

c# HttpWebRequest 异步请求_c#异步请求

c#异步请求

c# HttpWebRequest 异步请求

rainAlgo.uuid = this.algoProject.UUID;
trainAlgo.task = this.algoProject.Task;
trainAlgo.projectName = this.tb_projectName.Text.Trim();
trainAlgo.epoch = this.tb_epoch.Text.Trim();
trainAlgo.imgsz = this.tb_height.Text.Trim();
trainAlgo.device = this.tb_device.Text.Trim();
trainAlgo.batch = this.tb_batch.Text.Trim();
trainAlgo.weights = this.tb_weights.Text.Trim();
trainAlgo.save_loacal_path = "";
trainAlgo.project = this.tb_SaveDir.Text.Trim();
trainAlgo.data_yaml = this.dataYamlSPath;
trainAlgo.algoCategory = this.tb_algoCategory.Text.Trim();

// 2. 发送请求(初始化模型)
string url = "http://127.0.0.1:5000/init_algo";

JObject train_param = JObject.FromObject(trainAlgo);

// 初始化模型
api.GET_V2(url, train_param);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
public void GET_V2(string url, JObject param)
{
    try
    {
        // 拼接访问字符串
        if (param != null) //有参数的情况下,拼接url
        {
            url = url + "?";
            foreach (var item in param)
            {
                url = url + item.Key + "=" + item.Value + "&";
            }
            url = url.Substring(0, url.Length - 1);
        }

        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;//创建请求
        request.Method = "GET";//这里是POST请求,可以改成GET
        request.ContentType = "application/x-www-form-urlencoded";
        request.BeginGetResponse(new AsyncCallback(Compleate), request);
    }
    catch
    {
        MessageBox.Show("请求失败.");
    }
}

public static void Compleate(IAsyncResult asyncResult)
{
    try
    {
        HttpWebRequest req = (asyncResult.AsyncState as HttpWebRequest);
        HttpWebResponse res = req.EndGetResponse(asyncResult) as HttpWebResponse;
        StreamReader reader = new StreamReader(res.GetResponseStream());

        MessageBox.Show(reader.ReadToEnd());
    }
    catch
    {
        MessageBox.Show("获取失败.");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
public static void HttpPost(string Url)
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

        request.Method = "POST";//这里是POST请求,可以改成GET
        request.ContentType = "application/x-www-form-urlencoded";
        request.BeginGetResponse(new AsyncCallback(Compleate), request);
    }
    catch
    {
        MessageBox.Show("请求失败.");
    }
}

public static void Compleate(IAsyncResult asyncResult)
{
    try
    {
        HttpWebRequest req = (asyncResult.AsyncState as HttpWebRequest);
        HttpWebResponse res = req.EndGetResponse(asyncResult) as HttpWebResponse;
        StreamReader reader = new StreamReader(res.GetResponseStream());

        MessageBox.Show(reader.ReadToEnd());
    }
    catch
    {
        MessageBox.Show("获取失败.");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/259493
推荐阅读
相关标签
  

闽ICP备14008679号