简体   繁体   English

Apache HttpClient进程HttpResponse

[英]Apache HttpClient process HttpResponse

I'm trying to use Google's URL shortener API. 我正在尝试使用Google的URL缩短程序API。

The response I get should look like: 我得到的响应应类似于:

200

cache-control:  no-cache, no-store, max-age=0, must-revalidate
content-encoding:  gzip
content-length:  106
content-type:  application/json; charset=UTF-8
date:  Thu, 07 Dec 2017 23:39:07 GMT
etag:  "qQqhpr1RL6vGc3-0yacNoUjh_Uc/W5VD-15ZqaQDW9L-OELlMzo1ih4"
expires:  Mon, 01 Jan 1990 00:00:00 GMT
pragma:  no-cache
server:  GSE
vary:  Origin, X-Origin

{
 "kind": "urlshortener#url",
 "id": "[SHORTENED URL HERE]",
 "longUrl": "http://www.facebook.com/"
}

I want to be able to process the Json returned so that I can access the 'id' field and get the shortened URL. 我希望能够处理返回的Json,以便可以访问“ id”字段并获取缩短的URL。

However the response that I am getting looks like this: 但是我得到的响应如下所示:

Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Fri, 08 Dec 2017 10:10:37 GMT
ETag: "qQqhpr1RL6vGc3-0yacNoUjh_Uc/W5VD-15ZqaQDW9L-OELlMzo1ih4"
Vary: Origin
Vary: X-Origin
Content-Type: application/json; charset=UTF-8
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alt-Svc: hq=":443"; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=":443"; ma=2592000; v="41,39,38,37,35"
Transfer-Encoding: chunked 

I would appreciate it if someone could help me to figure out how to get access to the 'id' Json field so that I can get the shortened URL. 如果有人可以帮助我弄清楚如何访问“ id” Json字段,以便获得缩短的URL,我将不胜感激。

My code for this is: 我的代码是:

HttpPost httppost = new HttpPost("https://www.googleapis.com/urlshortener/v1/url?key=[MY_API_KEY]);
String jsondata = "{\"longUrl\": \"http://www.facebook.com/\"}";

StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setContentType("application/json;charset=utf-8");
jsonparam.setChunked(false);

httppost.addHeader("content-type", "application/json;charset=UTF-8");
httppost.setEntity(jsonparam);

HttpClient httpclient = HttpClientBuilder.create().build();
HttpResponse httpresponse = httpclient.execute(httppost);

Header[] headers = httpresponse.getAllHeaders();
for (Header header : headers) {
    System.out.println(header);
}

To read the body content you should do the following 要阅读身体内容,您应该执行以下操作

try(InputStream content = httpresponse.getEntity().getContent())
        {
            //With apache
            String jsonResponse = IOUtils.toString(content, "UTF-8");
            System.out.println(jsonResponse);
        } catch (UnsupportedOperationException | IOException e) 
        {
            //Do something here, e.g. LOG
        }

If you are using apache IOUtil you will need the following dependency https://mvnrepository.com/artifact/commons-io/commons-io 如果使用的是apache IOUtil,则需要以下依赖项https://mvnrepository.com/artifact/commons-io/commons-io

It is good practice to validate the returned status code to ensure that content is included eg 200. 优良作法是验证返回的状态码以确保包括内容,例如200。

Other methods of transforming the InputStream to a String can be found here . 这里可以找到将InputStream转换为String的其他方法。

Once loaded to a String you can use libraries such as GSON or using help from here . 加载到String后,您可以使用GSON之类的库,也可以使用此处的帮助。 By converting to a Java object you can more easily retrieve the id field. 通过转换为Java对象,您可以更轻松地检索id字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM