HttpClient 请求 URL字符集转码问题

 更新时间:2021年01月11日 14:29:04   作者:jinxiaoshao  
这篇文章主要介绍了HttpClient 请求 URL字符集转码问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

问题是这样的,我用eclipse发送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 = "中科洛阳信息产业园项目(一期)";
    String url = "http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/"+projectName ;
    HttpGet get = new HttpGet(url);
    try {
 
      // 创建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/中科洛阳信息产业园项目(一期) 放在谷歌浏览器,然后再复制出来,发现汉字编码格式变了。ok,那就先转换下编码格式再发送请求。  修改后代码如下:

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 = "中科洛阳信息产业园项目(一期)";
    String url = "http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/"+java.net.URLEncoder.encode(projectName, ENCODE);//URL 中文 转码
    HttpGet get = new HttpGet(url);
    try {
 
      // 创建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字符集转码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用Springboot整合GridFS实现文件操作

    使用Springboot整合GridFS实现文件操作

    这篇文章主要介绍了使用Springboot整合GridFS实现文件操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Java结合Swing实现龙年祝福语生成工具

    Java结合Swing实现龙年祝福语生成工具

    Swing是一个为Java设计的GUI工具包,属于Java基础类的一部分,本文将使用Java和Swing实现龙年祝福语生成工具,感兴趣的小伙伴可以了解下
    2024-01-01
  • java中单例模式讲解

    java中单例模式讲解

    这篇文章主要介绍了java中单例模式,本文通过简单的案例,讲解了该模式在java中的使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • MyBatis动态SQL之<choose><when><otherwise>标签的使用

    MyBatis动态SQL之<choose><when><otherwise>标签的使用

    MyBatis中动态语句choose-when-otherwise 类似于Java中的switch-case-default语句,本文就来介绍一下MyBatis动态SQL之<choose><when><otherwise>标签的使用,感兴趣的可以了解一下
    2023-09-09
  • Java中的Static class详解及实例代码

    Java中的Static class详解及实例代码

    这篇文章主要介绍了 Java中的Static class详解及实例代码的相关资料,在Java中我们可以有静态实例变量、静态方法、静态块。类也可以是静态的,需要的朋友可以参考下
    2017-03-03
  • Freemarker中的3种循环模式

    Freemarker中的3种循环模式

    这篇文章主要介绍了Freemarker中的3种循环模式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • java 自动生成略缩图示例代码

    java 自动生成略缩图示例代码

    本篇文章,在前辈的经验基础上,分别对单图生成略缩图和批量生成略缩图做个小结
    2013-07-07
  • Java for循环详解

    Java for循环详解

    这篇文章主要介绍了Java for循环方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • 深入学习java8 中的CompletableFuture

    深入学习java8 中的CompletableFuture

    本文主要介绍了java8中的CompletableFuture,CompletableFuture实现了CompletionStage接口和Future接口,前者是对后者的一个扩展,增加了异步回调、流式处理、多个Future组合处理的能力,使Java在处理多任务的协同工作时更加顺畅便利,下文需要的朋友可以参考一下
    2022-05-05
  • Spring Cloud 通过 Gateway webflux实现网关异常处理

    Spring Cloud 通过 Gateway webflux实现网关异常处理

    在某一个服务中出现异常,通过@ControllerAdvice + @ExceptionHandler 统一异常处理,即使在微服务架构中,也可以将上述统一异常处理放入到公共的微服务中,这样哪一个微服务需要,直接引入模块,本文重点介绍Spring Cloud 通过 Gateway webflux实现网关异常处理,一起看看吧
    2023-11-11

最新评论