问题是这样的,我用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字符集转码内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!