介绍
怎么对HttpClient请求的URL进行字符集转码?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
package com.vol.timingTasks; , , import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; , import java.io.IOException; ,/* * ,*数据抽取测试类 ,* ,* @author xbx ,* ,*/public class  XBXmain { private 才能final static  String ENCODE =,“utf-8"; , public 才能;static  void main (String [], args), throws Exception { getDataA (); ,,} , , ,/* ,,*,基本验证 ,,*,用户名: ,,*,密钥: ,,*/public 才能static void  getDataA (), throws 异常{ ,,,HttpResponse HttpResponse =,空; ,,,HttpClient HttpClient =, new DefaultHttpClient (); ,,,String projectName =,“中科洛阳信息产业园项目(一期)“; ,,,String url =,“http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/" + projectName ; ,,,HttpGet get =, new HttpGet (url); ,,,try { , ,,,,,//,创建HttpClientBuilder ,,,,,HttpClientBuilder HttpClientBuilder =, HttpClientBuilder.create (); ,,,,,//,设置BasicAuth ,,,,,CredentialsProvider provider =, new BasicCredentialsProvider (); ,,,,,//,Create 从而authentication 范围 ,,,,,AuthScope scope =, new AuthScope (AuthScope.ANY_HOST, AuthScope.ANY_PORT,, AuthScope.ANY_REALM); ,,,,,//,Create credential 一对,在此处填写用户名和密码 ,,,,,UsernamePasswordCredentials credentials =, new UsernamePasswordCredentials (“,,,,,); ,,,,,//,Inject 从而凭证 ,,,,,provider.setCredentials(范围,凭证); ,,,,,//,Set 从而default credentials 提供者 ,,,,,httpClientBuilder.setDefaultCredentialsProvider(供应商); ,,,,,//HttpClient ,,,,,CloseableHttpClient CloseableHttpClient =, httpClientBuilder.build (); , , ,,,,,httpResponse =, closeableHttpClient.execute(得到); ,,,,,HttpEntity HttpEntity =, httpResponse.getEntity (); ,,,,,String httpResult =, EntityUtils.toString (httpEntity); ,,,,,String httpResult2 =, EntityUtils.toString (httpEntity); ,,,},catch (IOException e), { ,,,} , ,,} , }
把访问地址:http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/中科洛阳信息产业园项目(一期)放在谷歌浏览器,然后再复制出来,发现汉字编码格式变了对吧,那就先转换下编码格式再发送请求又是;修改后代码如下:
package com.vol.timingTasks; , import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; , import java.io.IOException; ,/* * ,*数据抽取测试类 ,* ,* @author xbx ,* ,*/public class  XBXmain { private 才能final static  String ENCODE =,“utf-8"; , null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null怎么对HttpClient请求的URL进行字符集转码