HttpClient 请求 URL字符集转码问题
- 作者: 我的天呐
- 来源: 51数据库
- 2021-08-06
问题是这样的,我用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 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) {
}
}
}
再试试,请求成功,只需要转下编码:
string url = "http://labour.ztjs.cn/clound/wsforthird/laboursbyprojectname/"+java.net.urlencoder.encode(projectname, encode);//url 中文 转码
到此这篇关于httpclient 请求 url字符集转码问题的文章就介绍到这了,更多相关httpclient 请求 url字符集转码内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
- SQLite数据库操作:原生操作,GreenDao操作讲解
- 数据库SQL实战题:获取员工其当前的薪水比其manager当前薪水还高的相关信息(教程)
- SQLSERVER查询区分大小写的写法分析
- SQL学习总结之SQL的分类介绍
- 阶梯到高级T-SQL 1级:高级T-SQL介绍交叉连接
- 高级T-SQL级别1的Stairway:使用CROSS JOIN引入高级T-SQL分析
- 什么是SQL隔离级别?四个SQL隔离级别定义介绍
- Sql递归介绍之用with实现递归查询
- SQLSERVER查询时日期格式化的实例讲解
- 数据库SQL实战:从titles表获取按照title进行分组,注意对于重复的emp_no进行忽略(题解)
热点文章
SQLite数据库操作:原生操作,GreenDao操作讲解
18
数据库SQL实战题:获取员工其当前的薪水比其manager当前薪水还高的相关信息(教程)
4
SQLSERVER查询区分大小写的写法分析
36
SQL学习总结之SQL的分类介绍
6
阶梯到高级T-SQL 1级:高级T-SQL介绍交叉连接
4
高级T-SQL级别1的Stairway:使用CROSS JOIN引入高级T-SQL分析
4
什么是SQL隔离级别?四个SQL隔离级别定义介绍
2
Sql递归介绍之用with实现递归查询
6
SQLSERVER查询时日期格式化的实例讲解
4
数据库SQL实战:从titles表获取按照title进行分组,注意对于重复的emp_no进行忽略(题解)
7
