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;
/**
* 數據抽取測試類
*
* @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;
/**
* 數據抽取測試類
*
* @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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Springboot整合GridFS實現(xiàn)文件操作
這篇文章主要介紹了使用Springboot整合GridFS實現(xiàn)文件操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
MyBatis動態(tài)SQL之<choose><when><otherwise>標簽的使用
MyBatis中動態(tài)語句choose-when-otherwise 類似于Java中的switch-case-default語句,本文就來介紹一下MyBatis動態(tài)SQL之<choose><when><otherwise>標簽的使用,感興趣的可以了解一下2023-09-09
Spring?Cloud?通過?Gateway?webflux實現(xiàn)網關異常處理
在某一個服務中出現(xiàn)異常,通過@ControllerAdvice?+?@ExceptionHandler?統(tǒng)一異常處理,即使在微服務架構中,也可以將上述統(tǒng)一異常處理放入到公共的微服務中,這樣哪一個微服務需要,直接引入模塊,本文重點介紹Spring?Cloud?通過?Gateway?webflux實現(xiàn)網關異常處理,一起看看吧2023-11-11

