如果我从浏览器调用它,请使用无效的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调用将按预期工作。
答案 0 :(得分:1)
IIS Express仅将默认绑定设置为localhost。但是,您可以通过手动编辑配置文件来更改此设置。
再添加一个绑定,如下所示......
<绑定>
< binding protocol =" http" bindingInformation =" :5000:本地主机" />
<! - 添加以下行 - >
< binding protocol =" http" bindingInformation =" :5000:*" />
< /绑定>