当前位置 : 首页 » 文章分类 :  开发  »  Apache-HttpComponents

Apache-HttpComponents

Apache HttpComponents笔记

官网
http://hc.apache.org/

日志中不断重复的报warn:
00:00:00.665 [http-nio-8380-exec-454] WARN o.a.h.c.p.ResponseProcessCookies - Cookie rejected [BAIDUID=”C303600CB4B1BEDDCC9551003BD54DD5:FG=1”, version:0, domain:baidu.com, path:/, expiry:Wed Jan 12 00:00:46 CST 2022] Illegal ‘domain’ attribute “baidu.com”. Domain of origin: “10.123.123.21”

原因:服务器端 cookie 的 domain 是 10.123.123.21, 但客户端给传了个 baidu.com,两者不一致导致Cookie rejected。
domain是用来在浏览器端限制哪个域名可以访问此cookie内容的。我们使用httpclient一般来说是不需要用到cookie的。
为了不让日志不停的打印,可以设置HttpClient的Cookie策略:

RequestConfig requestConfig = = RequestConfig.custom()
                    // 设置Cookie策略为忽略
                    .setCookieSpec(CookieSpecs.STANDARD_STRICT)
                    .build();

从HTTPResponse中读取Json

利用 BufferedReader.readLine() 从 HTTPResponse.getEntity().getContent() 流中逐行读取返回内容:

HttpResponse response; // some response object
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
    builder.append(line).append("\n");
}
JSONTokener tokener = new JSONTokener(builder.toString());
JSONArray finalResult = new JSONArray(tokener);

如果JSON实际上是一行,那么你也可以删除循环和builder:

HttpResponse response; // some response object
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONTokener tokener = new JSONTokener(json);
JSONArray finalResult = new JSONArray(tokener);

使用gson:

void getJson() throws IOException {
    HttpClient  httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("some url of json");
    HttpResponse httpResponse = httpClient.execute(httpGet);
    String response = EntityUtils.toString(httpResponse.getEntity());

    Gson gson = new Gson();
    MyClass myClassObj = gson.fromJson(response, MyClass.class);
}

使用实例:

HttpResponse httpResponse = UdsHttpClient.postForRawResponseWithHeader(server + QUERY_SO_ORDER_LIST_API, null, content, false, headersMap, logger);
checkState(httpResponse != null, "Artemis returned empty response");
checkState(httpResponse.getStatusLine() != null, "Artemis return error response");
checkState(httpResponse.getEntity() != null, "Artemis returned empty response entity");
checkState(httpResponse.getStatusLine().getStatusCode() != ARTEMIS_TOKEN_ERROR_CODE, "Artemis return token error code");
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
String result = reader.readLine();
QuerySOListResponse response = JSONUtils.readValue(result, QuerySOListResponse.class);

How do I parse JSON from a Java HTTPResponse?
https://stackoverflow.com/questions/2845599/how-do-i-parse-json-from-a-java-httpresponse

如何从Java HTTPResponse解析JSON?
https://codeday.me/bug/20170629/34297.html

上一篇 Java-枚举

下一篇 Cassandra笔记

阅读
评论
448
阅读预计2分钟
创建日期 2018-08-24
修改日期 2021-01-12
类别
标签

页面信息

location:
protocol:
host:
hostname:
origin:
pathname:
href:
document:
referrer:
navigator:
platform:
userAgent:

评论