HttpClient 請求 URL字符集轉(zhuǎn)碼問題
問題是這樣的,我用eclipse發(fā)送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; /** * 數(shù)據(jù)抽取測試類 * * @author xbx * */ public class XBXmain { private final static String ENCODE = "utf-8"; public static void main(String[] args) throws Exception { getDataA(); } /* * Basic驗(yàn)證 * 用戶名: * 密鑰: */ public static void getDataA() throws Exception{ HttpResponse httpResponse = null; HttpClient httpClient = new DefaultHttpClient(); String projectName = "中科洛陽信息產(chǎn)業(yè)園項(xiàng)目(一期)"; String url = "http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/"+projectName ; HttpGet get = new HttpGet(url); try { // 創(chuàng)建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // 設(shè)置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/中科洛陽信息產(chǎn)業(yè)園項(xiàng)目(一期) 放在谷歌瀏覽器,然后再復(fù)制出來,發(fā)現(xiàn)漢字編碼格式變了。ok,那就先轉(zhuǎn)換下編碼格式再發(fā)送請求。 修改后代碼如下:
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; /** * 數(shù)據(jù)抽取測試類 * * @author xbx * */ public class XBXmain { private final static String ENCODE = "utf-8"; public static void main(String[] args) throws Exception { getDataA(); } /* * Basic驗(yàn)證 * 用戶名: * 密鑰: */ public static void getDataA() throws Exception{ HttpResponse httpResponse = null; HttpClient httpClient = new DefaultHttpClient(); String projectName = "中科洛陽信息產(chǎn)業(yè)園項(xiàng)目(一期)"; String url = "http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/"+java.net.URLEncoder.encode(projectName, ENCODE);//URL 中文 轉(zhuǎn)碼 HttpGet get = new HttpGet(url); try { // 創(chuàng)建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // 設(shè)置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) { } } }
再試試,請求成功,只需要轉(zhuǎn)下編碼:
String url = "http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/"+java.net.URLEncoder.encode(projectName, ENCODE);//URL 中文 轉(zhuǎn)碼
到此這篇關(guān)于HttpClient 請求 URL字符集轉(zhuǎn)碼問題的文章就介紹到這了,更多相關(guān)HttpClient 請求 URL字符集轉(zhuǎn)碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring HandlerInterceptor實(shí)現(xiàn)原理代碼解析
這篇文章主要介紹了Spring HandlerInterceptor實(shí)現(xiàn)原理代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫工具
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04EVCache緩存在Spring Boot中的實(shí)戰(zhàn)示例
這篇文章主要介紹了EVCache緩存在Spring Boot中的實(shí)戰(zhàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12