HttpClient request URL character set transcoding

Keywords: Apache Java Eclipse Google

The problem is that I can send httpclient request with eclipse as follows. But I will return 400 in idea. Why not??? 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;

/**
 * Data extraction test class
 *
 * @author xbx
 *
 */
public class XBXmain {
    private final static String ENCODE = "utf-8";

    public static void main(String[] args) throws Exception {
		getDataA();
    }


    /*
     * Basic Verification
     * User name:
     * Key:
     */
    public static void getDataA() throws  Exception{
        HttpResponse httpResponse = null;
        HttpClient httpClient = new DefaultHttpClient();
        String projectName = "Luoyang Information Industrial Park Project(Phase I)";
        String url = "http://labour.ztjs.cn/clound/wsForThird/laboursByProjectName/"+projectName ;
        HttpGet get = new HttpGet(url);
        try {

            // Create HttpClientBuilder
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            // Set 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, where to fill in the user name and password
            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) {
        }

    }



}

Address: Http://labour.ztjs.cn/cloud/wsforthird/laboursbyprojectname/ Luoyang Information Industrial Park Project (phase I)

Put it in Google browser, and then copy it out, and find that the coding format of Chinese characters has changed. ok, first convert the encoding format and then send the request. The modified code is as follows:

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;

/**
 * Data extraction test class
 *
 * @author xbx
 *
 */
public class XBXmain {
    private final static String ENCODE = "utf-8";

    public static void main(String[] args) throws Exception {
		getDataA();
    }


    /*
     * Basic Verification
     * User name:
     * Key:
     */
    public static void getDataA() throws  Exception{
        HttpResponse httpResponse = null;
        HttpClient httpClient = new DefaultHttpClient();
        String projectName = "Luoyang Information Industrial Park Project(Phase I)";
        String url = "http://Labour. Ztjs. CN / cloud / wsforthird / laboursbyprojectname / "+ Java. Net. Urlencoder. Encode (ProjectName, encode); / / url Chinese transcoding
        HttpGet get = new HttpGet(url);
        try {

            // Create HttpClientBuilder
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            // Set 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, where to fill in the user name and password
            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) {
        }

    }



}

Try again. The request is successful. Just transfer the code:

String url = "http://labour.ztjs.cn/cloud/wsforthird/laboursbyprojectname / + java.net.urlencoder.encode (ProjectName, encode); / / url Chinese transcoding

Posted by Bobo the Bugbear on Tue, 22 Oct 2019 09:08:38 -0700