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();
string url = "http://127.0.0.1:5000/init_algo";
JObject train_param = JObject.FromObject(trainAlgo);
api.GET_V2(url, train_param);
public void GET_V2(string url, JObject param)
{
try
{
if (param != null)
{
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";
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("获取失败.");
}
}
public static void HttpPost(string Url)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
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("获取失败.");
}
}