HttpClient 请求 URL字符集转码问题
问题是这样的,我用eclipse发送httpclient请求如下没有问题,但是在idea中就返回400,为毛呢???excuseme?
packagecom.vol.timingTasks;
importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.auth.AuthScope;
importorg.apache.http.auth.UsernamePasswordCredentials;
importorg.apache.http.client.CredentialsProvider;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.impl.client.BasicCredentialsProvider;
importorg.apache.http.impl.client.CloseableHttpClient;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.impl.client.HttpClientBuilder;
importorg.apache.http.util.EntityUtils;
importjava.io.IOException;
/**
*数据抽取测试类
*
*@authorxbx
*
*/
publicclassXBXmain{
privatefinalstaticStringENCODE="utf-8";
publicstaticvoidmain(String[]args)throwsException{
		getDataA();
}
/*
*Basic验证
*用户名:
*密钥:
*/
publicstaticvoidgetDataA()throwsException{
HttpResponsehttpResponse=null;
HttpClienthttpClient=newDefaultHttpClient();
StringprojectName="中科洛阳信息产业园项目(一期)";
Stringurl="http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/"+projectName;
HttpGetget=newHttpGet(url);
try{
//创建HttpClientBuilder
HttpClientBuilderhttpClientBuilder=HttpClientBuilder.create();
//设置BasicAuth
CredentialsProviderprovider=newBasicCredentialsProvider();
//Createtheauthenticationscope
AuthScopescope=newAuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT,AuthScope.ANY_REALM);
//Createcredentialpair,在此处填写用户名和密码
UsernamePasswordCredentialscredentials=newUsernamePasswordCredentials("","");
//Injectthecredentials
provider.setCredentials(scope,credentials);
//Setthedefaultcredentialsprovider
httpClientBuilder.setDefaultCredentialsProvider(provider);
//HttpClient
CloseableHttpClientcloseableHttpClient=httpClientBuilder.build();
httpResponse=closeableHttpClient.execute(get);
HttpEntityhttpEntity=httpResponse.getEntity();
StringhttpResult=EntityUtils.toString(httpEntity);
StringhttpResult2=EntityUtils.toString(httpEntity);
}catch(IOExceptione){
}
}
}
把访问地址:http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/中科洛阳信息产业园项目(一期)放在谷歌浏览器,然后再复制出来,发现汉字编码格式变了。ok,那就先转换下编码格式再发送请求。 修改后代码如下:
packagecom.vol.timingTasks;
importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.auth.AuthScope;
importorg.apache.http.auth.UsernamePasswordCredentials;
importorg.apache.http.client.CredentialsProvider;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.impl.client.BasicCredentialsProvider;
importorg.apache.http.impl.client.CloseableHttpClient;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.impl.client.HttpClientBuilder;
importorg.apache.http.util.EntityUtils;
importjava.io.IOException;
/**
*数据抽取测试类
*
*@authorxbx
*
*/
publicclassXBXmain{
privatefinalstaticStringENCODE="utf-8";
publicstaticvoidmain(String[]args)throwsException{
		getDataA();
}
/*
*Basic验证
*用户名:
*密钥:
*/
publicstaticvoidgetDataA()throwsException{
HttpResponsehttpResponse=null;
HttpClienthttpClient=newDefaultHttpClient();
StringprojectName="中科洛阳信息产业园项目(一期)";
Stringurl="http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/"+java.net.URLEncoder.encode(projectName,ENCODE);//URL中文转码
HttpGetget=newHttpGet(url);
try{
//创建HttpClientBuilder
HttpClientBuilderhttpClientBuilder=HttpClientBuilder.create();
//设置BasicAuth
CredentialsProviderprovider=newBasicCredentialsProvider();
//Createtheauthenticationscope
AuthScopescope=newAuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT,AuthScope.ANY_REALM);
//Createcredentialpair,在此处填写用户名和密码
UsernamePasswordCredentialscredentials=newUsernamePasswordCredentials("","");
//Injectthecredentials
provider.setCredentials(scope,credentials);
//Setthedefaultcredentialsprovider
httpClientBuilder.setDefaultCredentialsProvider(provider);
//HttpClient
CloseableHttpClientcloseableHttpClient=httpClientBuilder.build();
httpResponse=closeableHttpClient.execute(get);
HttpEntityhttpEntity=httpResponse.getEntity();
StringhttpResult=EntityUtils.toString(httpEntity);
StringhttpResult2=EntityUtils.toString(httpEntity);
}catch(IOExceptione){
}
}
}
再试试,请求成功,只需要转下编码:
Stringurl="http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/"+java.net.URLEncoder.encode(projectName,ENCODE);//URL 中文转码
到此这篇关于HttpClient请求URL字符集转码问题的文章就介绍到这了,更多相关HttpClient请求URL字符集转码内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!