HttpPut获取完整的请求字符串

时间:2013-03-20 14:15:33

标签: android httprequest

我尝试使用以下代码向服务器发出put请求:

    HttpClient client = new DefaultHttpClient();
    HttpPut put= new HttpPut(SERVER_URL + "/subscribe");

    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("UserId", params.get("UserId")));
    pairs.add(new BasicNameValuePair("Endpoint", params.get("Endpoint")));
    pairs.add(new BasicNameValuePair("SessionKey", params.get("SessionKey")));
    pairs.add(new BasicNameValuePair("SessionSecret", params.get("SessionSecret")));
    pairs.add(new BasicNameValuePair("Token", params.get("Token")));
    pairs.add(new BasicNameValuePair("CultureInfo", params.get("CultureInfo")));
    put.setEntity(new UrlEncodedFormEntity(pairs));
    put.setHeader("Content-Type", "application/json");
    HttpResponse response = client.execute(put);
    response.getStatusLine();

服务器在getStatusLine方法中返回500个代码响应,因此我想知道完整的请求文本以查看创建的请求。有什么建议可以吗?

1 个答案:

答案 0 :(得分:1)

为WIRE启用logs并为HEADERS

启用日志

您需要阅读上面的帖子,以便了解如何切换您尝试测试的连接的完整http日志。正如您在下面的示例中所看到的,一旦获得日志,您就可以看到标题中的所有内容以及所有内容(客户端到服务器和服务器到客户端)

使用CURL的建议 - 如果您开发使用这些工具,您可以考虑使用curl客户端,以便在编写android之前对客户端/服务器之间的对话进行建模。然后,借助android日志,您可以通过android发送,正是您从CLI curl客户端发送的内容,从而避免RC = 500的所有模糊问题。

打开日志记录的详细信息:

./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "HTTP/1.1 200 OK[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "Content-Type: application/json; charset=utf-8[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "Content-Disposition: attachment[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "Date: Fri, 16 Nov 2012 01:05:15 GMT[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "Expires: Fri, 16 Nov 2012 01:05:15 GMT[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "Cache-Control: private, max-age=0[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "X-Content-Type-Options: nosniff[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "X-Frame-Options: SAMEORIGIN[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "X-XSS-Protection: 1; mode=block[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "Server: GSE[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "Transfer-Encoding: chunked[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "71[\r][\n]"
./speechapi_1:D/ch.boye.httpclientandroidlib.wire( 6461): << "{"status":0,"id":"d70bb25a44fe84fb8bf7acf10c73b869-1","hypotheses":[{"
utterance":"hi","confidence":0.83725035}]}[\n]"