奇妙な問題があります。私の Android アプリから、ASP.NET Web API を呼び出しています。アプリから問題なく POST リクエストを行うことができます。ただし、GET リクエストを行うと、リクエストは「400 bad request invalid hostname」を返します。get ではなく POST リクエストを作成できるため、これは奇妙です。私が使用している URL はhttp://10.0.2.2/webapp/api/controllername
(Android エミュレーターが 127.0.0.1 を使用するため、10.0.2.2 です。これは公開された回避策です)。インターネット許可を有効にしています。
現在デバッグ中の Web API があり、投稿要求をデバッグできます。get 要求の URL を Internet Explorer に手動で入力して、サーバーにアクセスできます。これは、私がそれが機能していることを知っている方法です。サーバーにアクセスしていることを確認するためだけに、application_beginrequest のハンドラーがあり、それはポスト リクエスト用です。しかし、get リクエストがサーバーに着信することさえありません。
Android GET リクエストを作成するために、以下を利用しました (追加情報を含むコメントを追加しました)。
//Creation of get request
HttpGet request = new HttpGet(url); //correct URL; verified
for(NameValuePair h : headers)
{
//6 headers added; verified OK
request.addHeader(h.getName(), h.getValue());
}
//Calls method shown below
return executeRequest(request, url);
private ResponseInformation executeRequest(HttpUriRequest request, String url) throws Exception
{
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try {
//Get the response and check the code/message
httpResponse = client.execute(request);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
ResponseInformation info; // just a DTO for my needs
if (entity != null) {
InputStream instream = entity.getContent();
//Response is an XML document listing the 400 error information
response = convertStreamToString(instream);
info = new ResponseInformation(responseCode, response, message);
instream.close();
}
else
info = new ResponseInformation(responseCode, message);
return info;
}
.
.
}
誰が拒否しているのかわかりませんか?このエラーが発生するのはなぜですか? Application_BeginRequest にヒットすることはないため、Web API サービスには到達しませんが、POSTS は...とても奇妙です。