C# HttpClient调用保持等待模式,但在控制台应用程序中工作正常
下面是我在控制台应用程序上执行它时工作得非常好的代码。 线路C# HttpClient调用保持等待模式,但在控制台应用程序中工作正常,c#,asp.net-web-api,dotnet-httpclient,httpresponsemessage,C#,Asp.net Web Api,Dotnet Httpclient,Httpresponsemessage,下面是我在控制台应用程序上执行它时工作得非常好的代码。 线路 var postResponse=wait client.sendsync(req)在控制台应用程序中运行代码时给出结果 但当iam在WebApi控制器中使用此代码时,此代码会停在这一行上 using (var client = new HttpClient()) { var auth = "MTAwNDgucnVsZXNlbmdpbmUuc2VydmljZTp2N3FuY3I4cWlz"
var postResponse=wait client.sendsync(req)在控制台应用程序中运行代码时给出结果 但当iam在WebApi控制器中使用此代码时,此代码会停在这一行上
using (var client = new HttpClient())
{
var auth = "MTAwNDgucnVsZXNlbmdpbmUuc2VydmljZTp2N3FuY3I4cWlz";
client.BaseAddress = new Uri("https://federation-sts-stage.accenture.com");
var req = new HttpRequestMessage(HttpMethod.Post, "https://federation-sts-stage.test.com/oauth/ls/connect/token");
var cont = new FormUrlEncodedContent(bodyContents);
cont.Headers.ContentType = new MediaTypeHeaderValue("application/json");
cont.Headers.ContentLength = Convert.ToInt64("125");
req.Content = cont;
req.Headers.Add("Authorization", "Basic " + auth);
try
{
var postResponse = await client.SendAsync(req); // this is where the code keeps on waiting but works fine in console app
postResponse.EnsureSuccessStatusCode();
responseContents = postResponse.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
var msg = ex.Message;
return msg;
}
var responseObject = JObject.Parse(responseContents);
return responseObject.Value<string>("access_token");
}
我不知道我在做什么
根据评论,我把从apicontroller调用的整个方法放在下面,这个方法在控制台应用程序中运行良好,但是当我从apicontroller调用这个方法时,它会继续运行
public async Task<string> RequestTokenFromIssuer(string username, string password)
{
var bodyContents = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" },
{ "userName", username },
{ "password", password},
{ "scope", "read_rulesengine write_rulesengine" }
};
string responseContents = string.Empty;
using (var client = new HttpClient())
{
var auth = "MTAwNDgucnVsZXNlbmdpbmUuc2VydmljZTp2N3FuY3I4cWlz";
client.BaseAddress = new Uri("https://federation-sts-stage.test.com");
var req = new HttpRequestMessage(HttpMethod.Post, "https://federation-sts-stage.test.com/oauth/ls/connect/token");
var cont = new FormUrlEncodedContent(bodyContents);
cont.Headers.ContentType = new MediaTypeHeaderValue("application/json");
cont.Headers.ContentLength = Convert.ToInt64("125");
req.Content = cont;
req.Headers.Add("Authorization", "Basic " + auth);
try
{
var postResponse = await client.SendAsync(req);
postResponse.EnsureSuccessStatusCode();
responseContents = postResponse.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
var msg = ex.Message;
return msg;
}
var responseObject = JObject.Parse(responseContents);
return responseObject.Value<string>("access_token");
}
}
公共异步任务RequestTokenFromIssuer(字符串用户名、字符串密码)
{
var bodyContents=新字典
{
{“授权类型”、“客户端凭据”},
{“用户名”,用户名},
{“密码”,密码},
{“范围”、“读取规则引擎写入规则引擎”}
};
string responseContents=string.Empty;
使用(var client=new HttpClient())
{
var auth=“mtawndgucnvszxnlbmdpbmuuc2vydmljztp2n3fuy3i4clz”;
client.BaseAddress=新Uri(“https://federation-sts-stage.test.com");
var req=新的HttpRequestMessage(HttpMethod.Post)https://federation-sts-stage.test.com/oauth/ls/connect/token");
var cont=新FormUrlEncodedContent(正文内容);
cont.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/json”);
cont.Headers.ContentLength=Convert.ToInt64(“125”);
要求内容=续;
请求标题添加(“授权”、“基本”+auth);
尝试
{
var postress=wait client.sendaync(req);
postResponse.EnsureSuccessStatusCode();
responseContents=postResponse.Content.ReadAsStringAsync().Result;
}
捕获(例外情况除外)
{
var msg=例如消息;
返回味精;
}
var responseObject=JObject.Parse(responseContents);
返回响应对象值(“访问令牌”);
}
}
我相信你的问题实际上是在线的
responseContents = postResponse.Content.ReadAsStringAsync().Result;
从来没有,从来没有在异步方法中这样做过。你应该像这样做:
responseContents = await postResponse.Content.ReadAsStringAsync();
您可以分享您的WebApi控制器代码吗?这是您的真实身份验证密钥吗?您是否能够使用Wireshark(或类似工具)来比较通过网络传输的数据?只是为了确认两人提交了相同的https请求?这不是您问题的原因,但您应该知道,在HttpClient中使用
语句是个坏主意,请参见。@Rpcoder很好,谢天谢地,您不同意我的意见是一种意见,而不是事实。指出您应该使用共享静态实例。相关引用:“HttpClient只需实例化一次,并在应用程序的整个生命周期内重复使用。以下情况可能会导致SocketException错误:1.根据请求创建新的HttpClient实例。2.服务器负载过重。”
控件没有达到这一行,正如我所说的,代码停在:var postress=await client.SendAsync(req);我正在等待。
responseContents = await postResponse.Content.ReadAsStringAsync();