HttpClient 请求 URL字符集转码问题
作者:jinxiaoshao 发布时间:2022-11-20 17:33:07
标签: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 中文 转码
来源:https://blog.csdn.net/xubenxismile/article/details/96440133


猜你喜欢
- vs2005中总是保留最近打开的项目和文件的记录,甚至是以删除的它也不删,-_-!下面介绍几种删除的方法:第一种:建立一个bat文件,以后双
- 1、概述首先和大家一起回顾一下Java 消息服务,在我之前的博客《Java消息队列-JMS概述》中,我为大家分析了:1.消息服务:一个中间件
- 这篇做了一个简单的时间轴控件。右侧的数据就是一个简单的字符串。问题还是有的,当右侧的文字长度不一样的时候就会有问题了。现在可以修改一下适配右
- c#创建圆形类,其中包括set,get方法using System;using System.Collections.Generic;usi
- 使用SwingWorker线程模式谨慎地使用并发机制对Swing开发人员来说非常重要。一个好的Swing程序使用并发机制来创建不会失去响应的
- using System; using System.IO; using System.Data; using System.Text; u
- 多线程经常访问同一资源可能造成什么问题竞态条件和死锁如果两个或多个线程访问相同的对象,或者访问不同步的共享状态 ,就会出现竞态条件;为了避免
- 如下所示:public static void main(String[] args) {String str1 = "刘烨,孙坚
- EntityWrapper使用解析1、项目中引入jar包,我这里使用Maven构建<dependency> &nbs
- 熟悉Eclipse的都知道Eclipse经常性的会出现一些莫名其妙的问题,有时候运行的好好的突然重启一下项目就莫名的报错,所以经常会用到cl
- 1、Redis 怎么保证高可用、有哪些集群模式主从复制、哨兵模式、集群模式。2、主从复制在当前最新的 Redis 6.0 中,主从复制的完整
- ReentrantLock锁ReentrantLock是Java中常用的锁,属于乐观锁类型,多线程并 * 况下。能保证共享数据安全性,线程间有
- mybatis多层级collection嵌套json结构第一步查询第一层查询,将第一层的id传递到第二层当条件查询
- Controller如何进行重定向Spring MVC中进行重定向本人知道的有两种方式方法返回的URI(相对路径)中加上"redi
- 一般很多项目不是在springcloud的环境中使用的,但是需要用到分布式配置中心来管理一些外部或者项目的配置,这个时候我们可以使用spri
- windows xp下配置JDK环境变量:1.安装JDK,安装过程中可以自定义安装目录等信息,例如我们选择安装目录为D:/java/jdk1
- SpringAOP 通过JoinPoint获取参数名和值在Java8之前,代码编译为class文件后,方法参数的类型固定,但是方法名称会丢失
- 本文实例讲述了Android7.0上某些PopuWindow出现显示位置不正确问题的解决方法。分享给大家供大家参考,具体如下:情景描述:在a
- 预览:捕捉声卡输出:实现音频可视化, 第一步就是获得音频采样, 这里我们选择使用计算机正在播放的音频作为采样源进行处理:NAudio 中,
- 面向对象三要素:封装、继承、多态。封装和继承,这两个比较好理解,但要理解多态的话,可就稍微有点难度了。今天,我们就来讲讲多态的理解。我们应该