我想使用webclient而不是httpclient。

编程入门 行业动态 更新时间:2024-10-28 19:19:24
本文介绍了我想使用webclient而不是httpclient。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下代码工作正常,我只想使用没有异步的webclient。 或者我们可以从下面的方法中删除asnc并使它像常规方法一样。 //使用控制台应用程序从program.cs调用方法

static void Main(string [] args) { AsyncCallbackUrlPost (f0c9b87f41fd0b7824ffdadeccefefefdd3c39929dbceca61b3450d497981a5eeeec354713d61263a71e3a953beebf62b12d6d9819ebb0593e7947766e8bef03,http:// localhost:54262 / api / Payload); Console.ReadLine(); }

//这个方法我想用webclient替换

public static async Task AsyncCallbackUrlPost(string loanBeamMessageHash,string callbackURL) { HttpResponseMessage httpResponseMessage = null; 尝试 { Uri objUri = new Uri(callbackURL); //创建一个Http客户端并设置我们想要的头文件 HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(application / json)); //将其添加到Response Header并将其命名为LoanBeam_MessageHash httpClient.DefaultRequestHeaders.Add(LoanBeam_MessageHash,loanBeamMessageHash); httpClient.DefaultRequestHeaders.Host = objUri.Host; StringContent theContent = new StringContent(loanBeamMessageHash,System.Text.Encoding.UTF8,application / json); //发布数据 httpResponseMessage = await httpClient.PostAsync(objUri,theContent); //句柄状态代码200 if(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine(HTTP状态成功:+ httpResponseMessage.ReasonPhrase); 返回; } 其他 { return; } } catch(exception ex) { Console.WriteLine(HTTP status error:+ httpResponseMessage.ReasonPhrase); } }

我尝试过:

我上面的代码工作正常,我只是想使用webclient而不是异步。

解决方案

WebClient文档中有一个示例。 WebClient类(System.Net) [ ^

This below code is working fine, i just wanted to do it using webclient without async. Or can we remove anyways asnc from below method and make it like regular method. // Calling method from program.cs using console application

static void Main(string[] args) { AsyncCallbackUrlPost("f0c9b87f41fd0b7824ffdadeccefefefdd3c39929dbceca61b3450d497981a5eeeec354713d61263a71e3a953beebf62b12d6d9819ebb0593e7947766e8bef03", "localhost:54262/api/Payload"); Console.ReadLine(); }

// This method i want to replace with webclient

public static async Task AsyncCallbackUrlPost(string loanBeamMessageHash, string callbackURL) { HttpResponseMessage httpResponseMessage = null; try { Uri objUri = new Uri(callbackURL); //Create an Http client and set the headers we want HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //Add it to Response Header and call it "LoanBeam_MessageHash" httpClient.DefaultRequestHeaders.Add("LoanBeam_MessageHash", loanBeamMessageHash); httpClient.DefaultRequestHeaders.Host = objUri.Host; StringContent theContent = new StringContent(loanBeamMessageHash, System.Text.Encoding.UTF8, "application/json"); //Post the data httpResponseMessage = await httpClient.PostAsync(objUri, theContent); //handle status code 200 if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine("HTTP status success: " + httpResponseMessage.ReasonPhrase); return; } else { return; } } catch (Exception ex) { Console.WriteLine("HTTP status error: " + httpResponseMessage.ReasonPhrase); } }

What I have tried:

My above code is working fine, i just wanted to do it using webclient without async.

解决方案

There is an example on the WebClient documentation. WebClient Class (System.Net)[^]

更多推荐

我想使用webclient而不是httpclient。

本文发布于:2023-11-02 13:16:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552504.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我想   而不是   webclient   httpclient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!