• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

HttpClient 请求 URL字符集转码问题

java 搞代码 4年前 (2022-01-05) 21次浏览 已收录 0个评论

这篇文章主要介绍了HttpClient 请求 URL字符集转码问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

问题是这样的,我用eclipse发送httpclient请求如下没有问题,但是在idea中就返回400,为毛呢???excuse me?

 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(); } /* * Basic验证 * 用户名: * 密钥: */ public static void getDataA() throws Exception{ HttpResponse httpResponse = null; 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 the authentication scope AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); // Create credential pair,在此处填写用户名和密码 UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("", ""); // Inject the credentials provider.setCredentials(scope, credentials); // Set the default credentials provider httpClientBuilder.setDefaultCredentialsProvider(provider); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); httpResponse = closeableHttpClient.execute(get); HttpEntity httpEntity = httpResponse.getEntity(); String httpResult = EntityUtils.toString(httpEntity); String httpResult2 = EntityUtils.toString(httpEntity); } catch (IOException e) { } } }

把 访问地址:http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/中科洛阳信息产业园项目(一期) 放在谷歌浏览器,然后再复制出来,发现汉字编码格式变了。ok,那就先转换下编码格式再发送请求。  修改后代码如下:

 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(); } /* * Basic验证 * 用户名: * 密钥: */ public static void getDataA() throws Exception{ HttpResponse httpResponse = null; HttpClient httpClient = new DefaultHttpClient(); String projectName = "中科洛阳信息产业园项目(一期)"; String url = "http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/"+java.net.URLEncoder.encode(projectName, ENCODE);//URL 中文 转码 HttpGet get = new HttpGet(url); try { // 创建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // 设置BasicAuth CredentialsProvider provider = new BasicCredentialsProvider(); // Create the authentication scope AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); // Create credential pair,在此处填写用户名和密码 UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("", ""); // Inject the credenti<span style="color:transparent">来源gaodai#ma#com搞*代#码网</span>als provider.setCredentials(scope, credentials); // Set the default credentials provider httpClientBuilder.setDefaultCredentialsProvider(provider); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); httpResponse = closeableHttpClient.execute(get); HttpEntity httpEntity = httpResponse.getEntity(); String httpResult = EntityUtils.toString(httpEntity); String httpResult2 = EntityUtils.toString(httpEntity); } catch (IOException e) { } } }

再试试,请求成功,只需要转下编码:

String url = “http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/”+java.net.URLEncoder.encode(projectName, ENCODE);//URL  中文 转码

以上就是HttpClient 请求 URL字符集转码问题的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:HttpClient 请求 URL字符集转码问题
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址