【问题标题】:ASP.Net Web API Request URI ErrorASP.Net Web API 请求 URI 错误
【发布时间】: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


    【解决方案1】:

    我不知道为什么会发生这种情况,但我在 Web 表单(不是 win 表单客户端)中遇到了类似问题。这是通过使用基本元标记解决的。我不确定这是否能解决您的问题,但您可以尝试一下。

    你可以像这样在 HttpClient 中使用基地址(如果你还没有这样做的话):

        using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://xyzxyz.azurewebsites.net/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
        HttpResponseMessage response = await client.GetAsync("api/user/1");
        if (response.IsSuccessStatusCode)
        {
            //add your code
        }
    }
    

    【讨论】:

    • 我的代码类似但略有不同。一方面,我不会将所有内容都放在“使用”块中。我也没有对 defaultrequestheaders 运行“清除”——看看我编辑的问题,如果你认为你的代码可能会呈现与我不同的结果,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 2020-11-13
    • 2018-06-30
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    相关资源
    最近更新 更多