私のマシンには、AndroidおよびIOSデバイスにサービスを提供するために開発した単純なWCFWebサービスがあります。
このサービスには、次のような1つの方法があります。
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/?message={message}")]
string ServiceMessage(string message);
私は3つのクライアントを持っています。1つは正常に動作するHttpWebRequestを使用する.NETテストクライアント、1つは正常に動作するIOSクライアント、もう1つは失敗するHttpPostクラスとHttpClientクラスで開発したAndroidクライアントです。
Fiddlerリバースプロキシを使用して、.netクライアントの出力をデバッグしました。
POST http://127.0.0.1:8888/Service1.svc/?message=_|JSON MESSAGE BODY|_HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:8888
Content-Length: 338
Expect: 100-continue
Connection: Keep-Alive
_|JSON MESSAGE BODY|_
一方、これはAndroidHTTPPostの出力です。
POST http://10.0.2.2:8888/Service1.svc/ HTTP/1.1
Content-Type: application/json; charset=utf-8
Content-Length: 139
Host: 10.0.2.2:8888
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
Expect: 100-Continue
message=_|JSON MESSAGE BODY|_
ご覧のとおり、.Netはメッセージパラメータを上部の投稿に配置し、メッセージ変数名を下部に配置しませんが、Androidはメッセージ本文を上部の投稿に配置せず、メッセージ変数名をボトム。
これは私のAndroidの郵便番号です、
HttpPost httpPost = new HttpPost(url);
String messageBody = "message=" + jsonMessageParameter;
StringEntity entity = new StringEntity(messageBody);
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
HttpResponse response = hc.execute(httpPost);
InputStream content = response.getEntity().getContent();
String json = ConvertStreamToString(content);
このコードで呼び出すと、サーバーメソッドが呼び出されますが、メッセージメソッドパラメーターはnullです。Uri.Builderクラスで遊んで、Androidの投稿にメッセージをヘッダーに配置させようとしましたが、うまくいきません。誰かがここで私を助けてくれるなら、私は何時間も何時間もこれに固執しています。
前もって感謝します 、
ジェームズ
編集 :
Androidコードを次のように変更しました:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("message", jsonMessageParameter));
HttpPost httpPost = new HttpPost(url);
UrlEncodedFormEntity urlEncodedFromEntity = new UrlEncodedFormEntity(
nameValuePairs);
urlEncodedFromEntity.setContentType(new BasicHeader("Content-Type",
"application/json; charset=utf-8"));
httpPost.setEntity(urlEncodedFromEntity);
InputStream postStream = httpPost.getEntity().getContent();
String postOutput = ConvertStreamToString(postStream);
HttpResponse response = hc.execute(httpPost);
InputStream content = response.getEntity().getContent();
String json = ConvertStreamToString(content);
しかし、それでもフィドラーの監視は次のとおりです。
POST http://10.0.2.2:8888/Service1.svc/ HTTP/1.1
Content-Length: 189
Content-Type: application/json; charset=utf-8
Host: 10.0.2.2:8888
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
Expect: 100-Continue
message=_|MESSAGE_JSON|_