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  中文 转码

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十&年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值