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图文并茂讲解登录拦截器

    SpringBoot图文并茂讲解登录拦截器

    其实spring boot拦截器的配置方式和springMVC差不多,只有一些小的改变需要注意下就ok了,下面这篇文章主要给大家介绍了关于如何在Springboot实现登陆拦截器功能的相关资料,需要的朋友可以参考下
    2022-06-06
  • Java getRealPath(

    Java getRealPath("/")与getContextPath()区别详细分析

    这篇文章主要介绍了Java getRealPath("/")与getContextPath()区别详细分析,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • MyBatis如何使用selectKey返回主键的值

    MyBatis如何使用selectKey返回主键的值

    这篇文章主要介绍了MyBatis如何使用selectKey返回主键的值,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 在Java中轻松使用工厂设计模式介绍

    在Java中轻松使用工厂设计模式介绍

    这篇文章主要介绍了在Java中轻松使用工厂设计模式介绍,工厂设计模式或工厂方法设计模式是一种广泛使用且易于理解的设计模式,文章通过围绕主题展开详细的内容介绍,感兴趣的朋友可以参考一下
    2022-09-09
  • java实现人员信息管理系统

    java实现人员信息管理系统

    这篇文章主要为大家详细介绍了java实现人员信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Java重写与重载之间的区别

    Java重写与重载之间的区别

    本文主要介绍了Java重写与重载之间的区别。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • Spring boot整合jsp和tiles模板示例

    Spring boot整合jsp和tiles模板示例

    这篇文章主要介绍了Spring boot整合jsp模板和tiles模板的示例演示过程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-03-03
  • Tomcat安装配置及Eclipse配置详解

    Tomcat安装配置及Eclipse配置详解

    给大家介绍一下Tomcat安装配置及Eclipse配置的全部图文过程,如果你对这个还有不明白,一起跟着小编学习下。
    2017-11-11
  • Java缓存ehcache的使用步骤

    Java缓存ehcache的使用步骤

    这篇文章主要介绍了Java缓存ehcache的使用步骤,文中有非常详细的代码示例,对正在学习java的小伙伴们有很好的帮助,需要的朋友可以参考下
    2021-05-05
  • 使用SpringBoot的CommandLineRunner遇到的坑及解决

    使用SpringBoot的CommandLineRunner遇到的坑及解决

    这篇文章主要介绍了使用SpringBoot的CommandLineRunner遇到的坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02

最新评论