【发布时间】:2014-05-08 22:28:19
【问题描述】:
我有一个通常可以正常工作的 ASP.Net Web API。我有一个执行 GET 请求的 Winforms 客户端应用程序。客户端应用程序在我们的公司网络上运行(API 托管为 Azure 网站)。有时,而且不一致的是,我进行的 HttpClient 调用会在我的 GET 调用前面添加似乎是公司 URL 的内容。
示例:我尝试调用将 HttpClient 请求发送到以下 URL:'http://xyzxyz.azurewebsites.net/api/user/1'
但实际提出的请求是:
'http://usgaabc1iru01/B0000D0000N0001F0000S0000R0004/http://xyzxyz.azurewebsites.net/api/user/1'
这显然会导致错误。
我已经询问了我们的 IT 部门可能会发生什么,但他们不知所措。希望有人能指出我正确的方向。
编辑:
这是我使用的代码。首先,我有一个静态方法,我调用所有调用 API 以获取 HttpClient 的东西(这可能是尴尬/糟糕):
public static HttpClient GetHttpClient()
{
var credentials = new NetworkCredential(GlobalVariables.CurrentUser.UserName, GlobalVariables.CurrentUser.Password);
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = false;
handler.Credentials = credentials;
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri(PublicClasses.GlobalVariables.BaseUriString);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
GlobalVariables.CredentialedHttpClient = client;
}
return GlobalVariables.CredentialedHttpClient;
}
}
这是我使用的一个简单的 GET 调用:
public static List<Project> GetAllProjects()
{
try
{
HttpClient client = GetHttpClient();
HttpResponseMessage response = client.GetAsync("api/project").Result; // Blocking call!
if (response.IsSuccessStatusCode)
{
var projects = response.Content.ReadAsAsync<IEnumerable<Project>>().Result;
return (List<Project>)projects;
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
return null;
}
}
catch (Exception)
{
throw;
}
}
【问题讨论】:
标签: c# asp.net azure network-programming asp.net-web-api