欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

HttpClient 請求 URL字符集轉(zhuǎn)碼問題

 更新時(shí)間:2021年01月11日 14:29:04   作者:jinxiaoshao  
這篇文章主要介紹了HttpClient 請求 URL字符集轉(zhuǎn)碼問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

問題是這樣的,我用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)文章

  • springboot如何集成mysql

    springboot如何集成mysql

    這篇文章主要介紹了springboot如何集成mysql問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Java實(shí)現(xiàn)記事本功能

    Java實(shí)現(xiàn)記事本功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)記事本功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Spring HandlerInterceptor實(shí)現(xiàn)原理代碼解析

    Spring HandlerInterceptor實(shí)現(xiàn)原理代碼解析

    這篇文章主要介紹了Spring HandlerInterceptor實(shí)現(xiàn)原理代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Java使用jmeter進(jìn)行壓力測試

    Java使用jmeter進(jìn)行壓力測試

    本篇文章簡單講一下使用jmeter進(jìn)行壓力測試。其壓測思想就是 通過創(chuàng)建指定數(shù)量的線程,同時(shí)請求指定接口,來模擬指定數(shù)量用戶同時(shí)進(jìn)行某個(gè)操作的場景,感興趣的小伙伴們可以參考一下
    2021-07-07
  • java實(shí)現(xiàn)圖片文字識別ocr

    java實(shí)現(xiàn)圖片文字識別ocr

    這篇文章主要介紹了java實(shí)現(xiàn)圖片文字識別ocr ,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-08-08
  • MyBatis批量插入數(shù)據(jù)過程解析

    MyBatis批量插入數(shù)據(jù)過程解析

    這篇文章主要介紹了MyBatis批量插入數(shù)據(jù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫工具

    java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫工具

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Maven模版Bug及解決辦法

    Maven模版Bug及解決辦法

    默認(rèn),會幫我們創(chuàng)建src/main/resources 按照Maven的規(guī)范,Maven會有3個(gè)目錄,分別是: src/main/java : java源文件存放位置 src/main/resource : resource資源,如配置文件等 src/test/java : 測試代碼源文件存放位置
    2016-04-04
  • 解決Maven靜態(tài)資源過濾問題

    解決Maven靜態(tài)資源過濾問題

    在我們使用Maven構(gòu)建項(xiàng)目的時(shí)候,會默認(rèn)過濾掉靜態(tài)資源,所以,需要手動來配置,本文就介紹一下Maven靜態(tài)資源過濾的問題解決,感興趣的可以了解一下
    2021-06-06
  • EVCache緩存在Spring Boot中的實(shí)戰(zhàn)示例

    EVCache緩存在Spring Boot中的實(shí)戰(zhàn)示例

    這篇文章主要介紹了EVCache緩存在Spring Boot中的實(shí)戰(zhàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12

最新評論