简体   繁体   English

ASP.Net Web API请求URI错误

[英]ASP.Net Web API Request URI Error

I have an ASP.Net Web API that generally works fine. 我有一个通常可以正常运行的ASP.Net Web API。 I have a Winforms client application that does GET requests. 我有一个执行GET请求的Winforms客户端应用程序。 The client application runs on our corporate network (the API is hosted as an Azure Website). 客户端应用程序在我们的公司网络上运行(API托管为Azure网站)。 Occasionally, and inconsistently, the HttpClient calls I make add what seem to be corporate URLs in front of my GET call. 有时,而且不一致地,我进行的HttpClient调用在GET调用之前添加了似乎是公司URL的内容。

Example: I try to call send an HttpClient request to the following URL: ' http://xyzxyz.azurewebsites.net/api/user/1 ' 示例:我尝试调用将HttpClient请求发送到以下URL:' http : //xyzxyz.azurewebsites.net/api/user/1 '

but the actual request made is: 但实际提出的要求是:

'http://usgaabc1iru01/B0000D0000N0001F0000S0000R0004/http://xyzxyz.azurewebsites.net/api/user/1'

This obviously causes an error. 这显然会导致错误。

I've asked our IT department what may be happening and they are at a loss. 我已经问过我们的IT部门,可能会发生什么,而他们茫然无措。 Hoping someone could point me in the right direction. 希望有人可以指出正确的方向。

Edit: 编辑:

Here's the code I use. 这是我使用的代码。 First I have a static method I call everything I make a call to the API to get the HttpClient (is this awkward/bad perhaps): 首先,我有一个静态方法,我调用所有调用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;
        }
   }

Here's a simple GET call I use: 这是我使用的一个简单的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;
        }
    }

I don't have an answer for why is this happening but I came across similar issue in a web form (not a win forms client). 我没有答案为什么会这样,但是我在Web表单中遇到了类似的问题(不是Win Forms客户端)。 That is solved by using base meta tag.I am not sure if that solves your problem, but you can give a try. 这可以通过使用基本的meta标签解决。我不确定是否能解决您的问题,但是您可以尝试一下。

You can use base address with HttpClient like this (if you are not already doing this): 您可以使用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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM