HttpClient 請(qǐng)求 URL字符集轉(zhuǎn)碼問(wèn)題
問(wèn)題是這樣的,我用eclipse發(fā)送httpclient請(qǐng)求如下沒(méi)有問(wèn)題,但是在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ù)抽取測(cè)試類
*
* @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 = "中科洛陽(yáng)信息產(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) {
}
}
}
把 訪問(wèn)地址:http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/中科洛陽(yáng)信息產(chǎn)業(yè)園項(xiàng)目(一期) 放在谷歌瀏覽器,然后再?gòu)?fù)制出來(lái),發(fā)現(xiàn)漢字編碼格式變了。ok,那就先轉(zhuǎn)換下編碼格式再發(fā)送請(qǐng)求。 修改后代碼如下:
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ù)抽取測(cè)試類
*
* @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 = "中科洛陽(yáng)信息產(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) {
}
}
}
再試試,請(qǐng)求成功,只需要轉(zhuǎn)下編碼:
String url = "http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/"+java.net.URLEncoder.encode(projectName, ENCODE);//URL 中文 轉(zhuǎn)碼
到此這篇關(guān)于HttpClient 請(qǐng)求 URL字符集轉(zhuǎn)碼問(wèn)題的文章就介紹到這了,更多相關(guān)HttpClient 請(qǐng)求 URL字符集轉(zhuǎn)碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Springboot整合GridFS實(shí)現(xiàn)文件操作
這篇文章主要介紹了使用Springboot整合GridFS實(shí)現(xiàn)文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java結(jié)合Swing實(shí)現(xiàn)龍年祝福語(yǔ)生成工具
Swing是一個(gè)為Java設(shè)計(jì)的GUI工具包,屬于Java基礎(chǔ)類的一部分,本文將使用Java和Swing實(shí)現(xiàn)龍年祝福語(yǔ)生成工具,感興趣的小伙伴可以了解下2024-01-01
MyBatis動(dòng)態(tài)SQL之<choose><when><o(jì)therwise>標(biāo)簽的使用
MyBatis中動(dòng)態(tài)語(yǔ)句choose-when-otherwise 類似于Java中的switch-case-default語(yǔ)句,本文就來(lái)介紹一下MyBatis動(dòng)態(tài)SQL之<choose><when><o(jì)therwise>標(biāo)簽的使用,感興趣的可以了解一下2023-09-09
Java中的Static class詳解及實(shí)例代碼
這篇文章主要介紹了 Java中的Static class詳解及實(shí)例代碼的相關(guān)資料,在Java中我們可以有靜態(tài)實(shí)例變量、靜態(tài)方法、靜態(tài)塊。類也可以是靜態(tài)的,需要的朋友可以參考下2017-03-03
深入學(xué)習(xí)java8?中的CompletableFuture
本文主要介紹了java8中的CompletableFuture,CompletableFuture實(shí)現(xiàn)了CompletionStage接口和Future接口,前者是對(duì)后者的一個(gè)擴(kuò)展,增加了異步回調(diào)、流式處理、多個(gè)Future組合處理的能力,使Java在處理多任務(wù)的協(xié)同工作時(shí)更加順暢便利,下文需要的朋友可以參考一下2022-05-05
Spring?Cloud?通過(guò)?Gateway?webflux實(shí)現(xiàn)網(wǎng)關(guān)異常處理
在某一個(gè)服務(wù)中出現(xiàn)異常,通過(guò)@ControllerAdvice?+?@ExceptionHandler?統(tǒng)一異常處理,即使在微服務(wù)架構(gòu)中,也可以將上述統(tǒng)一異常處理放入到公共的微服務(wù)中,這樣哪一個(gè)微服務(wù)需要,直接引入模塊,本文重點(diǎn)介紹Spring?Cloud?通過(guò)?Gateway?webflux實(shí)現(xiàn)網(wǎng)關(guān)異常處理,一起看看吧2023-11-11

