如果我不使用http:// localhost,Http客户端总是返回404

时间:2015-12-23 15:43:27

标签: c# iis web asp.net-web-api iis-8

如果我从浏览器调用它,请使用无效的web api。

我想用HttpClient来调用它:

 private const string BaseAddress = "http://13.195.151.169:80/VI_ProfilerWebServer/";

    private static readonly HttpClient HttpClient = new HttpClient();



    public static string PostAsJsonAsync(string route, Object jsonConverterParam)
    {

        string result = String.Empty;
        string jsonSerializable = JsonConvert.SerializeObject(jsonConverterParam, Formatting.Indented);
        JObject postContent = JObject.Parse(jsonSerializable);

        try
        {
            var fullRoute = PrepareHttpRequest(route);
            using (HttpResponseMessage response = HttpClient.PostAsJsonAsync(fullRoute, postContent).Result)
            {
                result = response.Content.ReadAsStringAsync().Result;
                DumpUtils.DumpDataToFile(route, fullRoute, postContent, result, response);
                response.EnsureSuccessStatusCode();
            }
        }
        catch (Exception ex)
        {
            throw new HttpRequestException(result, ex);
        }

        return result;
    }
    private static string PrepareHttpRequest(string route)
    {

        HttpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
        string requestUri = BaseAddress + route;
        return requestUri;
    }

此代码在baseurl为http://localhost地址的开发环境中正常工作。但是,只要我将baseUrl更改为http://13.195.151.169:80/VI_ProfilerWebServer/,我就会得到404.如果我使用浏览器并输入说http://13.195.151.169:80/VI_ProfilerWebServer/(代码构造的URI),API调用将按预期工作。

1 个答案:

答案 0 :(得分:1)

IIS Express仅将默认绑定设置为localhost。但是,您可以通过手动编辑配置文件来更改此设置。

  1. 右键单击IIS Express Tray
  2. 打开正在运行的应用程序
  3. 点击正在运行的应用程序,在底部将显示位于c:\ Users \ USERNAME \ Documents \ IISExpress \ config \ applicationHost.config
  4. 的配置文件
  5. 再添加一个绑定,如下所示......

    <绑定>
          < binding protocol =" http" bindingInformation =" :5000:本地主机" />
          <! - 添加以下行 - >
          < binding protocol =" http" bindingInformation ="
    :5000:*" />
      < /绑定>