我实际上是在尝试使用HTTPClient类通过 URL 获取数据。我通过 ajax 调用。但是,当获取响应时,它会从调试模式返回运行模式,并且没有任何反应。未检索到任何响应。下面是代码:
jQuery
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "../../Services/AService.asmx/GetCompanyInformation",
data: { id: JSON.stringify(id) },
contentType: "application/json; charset=utf-8",
dataType: "json",
async:true,
success: function (res) {
var options = JSON.parse(res.d);
},
error: function (errormsg) {
$(".dropdown_SubDomainLoading").hide();
toastr.options.timeOut = 7000;
toastr.options.closeButton = true;
toastr.error('Something Went Wrong');
}
});
网络方法调用
public static string CompanyDetails;
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string GetCompanyInformation(string id)
{
string authorizationKey = "Bearer 5cae498ef11f128363c1fbb2761bbab40ac0e2e5";
string url = string.Empty;
url = GetCompanyDetailsForApps(id);
RunAsync(url).Wait();
return CompanyDetails;
}
static async Task RunAsync(string Url)
{
string authorizationKey = "Bearer 5cae498ef11f128363c1fbb2761bbab40ac0e2e5";
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(Url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Authorization", authorizationKey);
HttpResponseMessage response = await client.GetAsync(Url);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
//UrlMetricsResponse mozResonse = JsonConvert.DeserializeObject<UrlMetricsResponse>(content);
dynamic dynObj = JsonConvert.DeserializeObject(content);
CompanyDetails = JsonConvert.SerializeObject(dynObj);
}
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR :" + ex.Message);
}
}
一旦调用了client.GetAsync()函数,它就会返回到运行模式,并且不会获取任何内容。难道我做错了什么。如何通过 url 检索响应?