url转uri的碰到的问题和解决办法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38500014/article/details/82850035

在用CloseableHttpClient进行get请求的时候,url转uri一直存在一些问题,后来仔细看了URL对象中的一些参数,琢磨了一个转换办法,代码如下,仅供参考

URL url = new URL(urlBuffer.toString());
URI uri = new URI(url.getProtocol(), url.getHost()+":"+url.getPort(), url.getPath(), url.getQuery(), null);
HttpGet httpGET = new HttpGet(uri);
httpGET.setHeader("Accept", "application/xml"); ////设置返回内容格式
HttpResponse response = httpclient.execute(httpGET);   //调用接口
int status = response.getStatusLine().getStatusCode();
com.alibaba.fastjson.JSONObject jsonObj = com.alibaba.fastjson.JSONObject.parseObject(EntityUtils.toString(response.getEntity()));

猜你喜欢

转载自blog.csdn.net/weixin_38500014/article/details/82850035
今日推荐