您好,登錄后才能下訂單哦!
怎么對HttpClient請求的URL進行字符集轉碼?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
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 中文 轉碼
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。