我正在尝试使用System.Net.Http.HttpClient PostAsync方法进行api调用。等待_httpClient.PostAsync(requestUri,stringContent);
后,它将永远挂起HttpClient配置:
def foo()
puts "First name: "
first_name = gets.chomp.capitalize
puts
puts "Last name: "
last_name = gets.chomp.capitalize
puts
puts "Age: "
input_age = gets.chomp
begin
age = Integer(input_age)
if age > 0 and age <= 130 then
puts
puts "You are #{first_name} #{last_name}. You have been living for #{input_age} years now. Is entered information correct? [y/n]"
true_false = gets.chomp
if true_false == "y"
puts
puts "Good!"
puts
puts ".........ENTERING STAGE~2........."
else true_false == "n"
puts
puts "Please correct the information and then proceed to next stage."
puts
foo()
end
end
end
end
first_name = foo()
puts
last_name = foo()
puts
age = foo()
puts "You are #{first_name} #{last_name}. You have been living for #{input_age} years now."
PostAsync调用:
_httpClient = new System.Net.Http.HttpClient(new HttpClientHandler
{
UseDefaultCredentials = WithDefaultCredentials,
AllowAutoRedirect = false
});
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(APPLICATION_JSON));
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(ApplicationXWwwFormUrlencoded));
if (!string.IsNullOrEmpty(CultureInfo.CurrentUICulture.Name))
{
_httpClient.DefaultRequestHeaders.AcceptLanguage.Add(new
StringWithQualityHeaderValue(CultureInfo.CurrentUICulture.Name));
}
答案 0 :(得分:1)
使用FormUrlEncodedContent而不是字符串内容解决了我的问题:
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("username", username),
// ...
});
var response = await _httpClient.PostAsync(requestUri, stringContent);
var responseContent = await response.Content.ReadAsStringAsync();