调用 ASP.NET webapi 时出现 404 File not found 异常
404 File not found exception when calling ASP.NET webapi
当我尝试 POST 从 android 到 asp.net web api 的参数时,我遇到了文件未找到异常。但是同一个网站 api 正在与邮递员合作。请建议我需要更正哪一部分?
Android 代码是:
public String CallWebAPI()
{
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("name", "Rahul")
.build();
Request request = new Request.Builder()
.url("http://www.xxxx.co/testapi/TestNameWEBApi")
.post(formBody)
.build();
Call call = client.newCall(request);
Response response = null;
try {
response = call.execute();
Log.e("ATTEST", "App1 Error is :" + response.toString());
} catch (IOException e) {
Log.e("ATTEST", "App1 IOException is :" + e.toString());
e.printStackTrace();
}
return response.toString();
}
WebAPI 是:
[RoutePrefix("testapi")]
[Route("TestNameWEBApi"), HttpPost]
public HttpResponseMessage TestNameWEBApi(string name)
{
try
{
var Response = name;
var Result = this.Request.CreateResponse(HttpStatusCode.OK, Response, new JsonMediaTypeFormatter());
return Result;//return same parameter to check if the value is reaching here or not
}
catch (Exception ex)
{
HttpError Error = new HttpError(ex.Message) { { "IsSuccess", false } };
return this.Request.CreateErrorResponse(HttpStatusCode.OK, Error);
}
}
您的请求缺少查询参数名称。
RequestBody formBody = new FormBody.Builder()
.add("name", "Rahul")
.build();
上面的代码将名称和数据添加到正文,而不是 URL。
您可能会收到 404 Not Found,因为您的服务器要求存在名称参数,否则它将与完整路由不匹配。
从正文中删除名称和数据,并将它们添加到 URL。
public String CallWebAPI() {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.build();
Request request = new Request.Builder()
.url("http://www.xxxx.co/testapi/TestNameWEBApi?name=Rahul")
.post(formBody)
.build();
Call call = client.newCall(request);
Response response = null;
try {
response = call.execute();
Log.e("ATTEST", "App1 Error is :" + response.toString());
} catch (IOException e) {
Log.e("ATTEST", "App1 IOException is :" + e.toString());
e.printStackTrace();
}
return response.toString();
}
在这种情况下,正文是完全空的,但是您可以在路由正常运行后添加它。
可能有更好的方法将查询参数添加到 URL,但这应该可行。
当我尝试 POST 从 android 到 asp.net web api 的参数时,我遇到了文件未找到异常。但是同一个网站 api 正在与邮递员合作。请建议我需要更正哪一部分?
Android 代码是:
public String CallWebAPI()
{
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("name", "Rahul")
.build();
Request request = new Request.Builder()
.url("http://www.xxxx.co/testapi/TestNameWEBApi")
.post(formBody)
.build();
Call call = client.newCall(request);
Response response = null;
try {
response = call.execute();
Log.e("ATTEST", "App1 Error is :" + response.toString());
} catch (IOException e) {
Log.e("ATTEST", "App1 IOException is :" + e.toString());
e.printStackTrace();
}
return response.toString();
}
WebAPI 是:
[RoutePrefix("testapi")]
[Route("TestNameWEBApi"), HttpPost]
public HttpResponseMessage TestNameWEBApi(string name)
{
try
{
var Response = name;
var Result = this.Request.CreateResponse(HttpStatusCode.OK, Response, new JsonMediaTypeFormatter());
return Result;//return same parameter to check if the value is reaching here or not
}
catch (Exception ex)
{
HttpError Error = new HttpError(ex.Message) { { "IsSuccess", false } };
return this.Request.CreateErrorResponse(HttpStatusCode.OK, Error);
}
}
您的请求缺少查询参数名称。
RequestBody formBody = new FormBody.Builder()
.add("name", "Rahul")
.build();
上面的代码将名称和数据添加到正文,而不是 URL。
您可能会收到 404 Not Found,因为您的服务器要求存在名称参数,否则它将与完整路由不匹配。
从正文中删除名称和数据,并将它们添加到 URL。
public String CallWebAPI() {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.build();
Request request = new Request.Builder()
.url("http://www.xxxx.co/testapi/TestNameWEBApi?name=Rahul")
.post(formBody)
.build();
Call call = client.newCall(request);
Response response = null;
try {
response = call.execute();
Log.e("ATTEST", "App1 Error is :" + response.toString());
} catch (IOException e) {
Log.e("ATTEST", "App1 IOException is :" + e.toString());
e.printStackTrace();
}
return response.toString();
}
在这种情况下,正文是完全空的,但是您可以在路由正常运行后添加它。
可能有更好的方法将查询参数添加到 URL,但这应该可行。