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