HttpClient 请求 URL字符集转码问题

作者:jinxiaoshao 时间:2022-11-20 17:33:07 

问题是这样的,我用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  中文 转码

来源:https://blog.csdn.net/xubenxismile/article/details/96440133

标签:HttpClient,URL,字符集,转码
0
投稿

猜你喜欢

  • JWT.net 操作实践方法

    2022-04-09 16:01:28
  • idea maven pom不自动更新的解决方法

    2021-11-19 14:56:46
  • 基于TCP异步Socket模型的介绍

    2022-08-05 23:43:25
  • 有关于整体刷新和局部刷新frameset窗口

    2023-04-21 22:43:40
  • Android仿微信清理内存图表动画(解决surfaceView屏幕闪烁问题)demo实例详解

    2023-05-31 15:35:27
  • android TabLayout使用方法详解

    2021-08-06 07:52:17
  • java实现简易外卖订餐系统

    2021-11-04 16:01:25
  • Android使用SoundPool播放短音效

    2022-03-02 12:33:00
  • springmvc实现自定义类型转换器示例

    2021-09-29 23:46:53
  • C#对WPF数据绑定的菜单插入Seperator分隔

    2022-04-29 17:09:28
  • 深入理解Java虚拟机体系结构

    2021-09-15 19:46:53
  • Android中实现iOS中的毛玻璃效果

    2023-01-27 01:44:14
  • 详解springboot中使用异步的常用两种方式及其比较

    2021-06-16 17:43:29
  • Andriod arcgis保存Mapview为图片的实例代码

    2022-08-15 01:23:29
  • Android Studio finish()方法的使用与解决app点击“返回”(直接退出)

    2022-07-31 09:37:10
  • C# .NET 中的缓存实现详情

    2023-05-30 18:35:12
  • 简单通用JDBC辅助类封装(实例)

    2021-10-14 23:09:28
  • Java枚举类用法实例

    2023-09-25 01:47:34
  • C语言数据结构之二叉树详解

    2021-08-18 20:56:41
  • Java 详解循环屏障CyclicBarrier如何实现多线程分段等待执行完成

    2022-02-16 04:50:49
  • asp之家 软件编程 m.aspxhome.com