本文介绍了我想使用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。
发布评论