java springboot http get請求 URLConnection get 返回值 亂碼
public static String sendGet(String url, String param)
{
StringBuilder result = new StringBuilder();
BufferedReader in = null;
try
{
String urlNameString = url + "?" + param;
log.info("sendGet - {}", urlNameString);
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("Charsert", "UTF-8"); //設定請求編碼
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null)
{
result.append(line);
}
log.info("recv - {}", result);
}
catch (ConnectException e)
{
log.error("呼叫HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
}
catch (SocketTimeoutException e)
{
log.error("呼叫HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
}
catch (IOException e)
{
log.error("呼叫HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
}
catch (Exception e)
{
log.error("呼叫HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
}
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (Exception ex)
{
log.error("呼叫in.close Exception, url=" + url + ",param=" + param, ex);
}
}
return result.toString();
}
上述程式碼
in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));解決亂碼問題
相關文章
- Java Http Get Post 請求工具類JavaHTTP
- HTTP GET請求傳bodyHTTP
- GET請求引數為中文時亂碼分析
- Python中get、post請求詳解(HTTP請求頭、狀態碼)PythonHTTP
- Go HTTP GET 請求可以傳送 body 嗎GoHTTP
- http請求之get和post的區別HTTP
- Go使用net/http庫傳送GET請求GoHTTP
- java傳送GET和post請求Java
- Servlet中request請求Get和Post方法以及亂碼解決Servlet
- Postman 使用 Get 請求 URL 傳參中文亂碼的問題Postman
- SpringBoot Get 請求接收 Date 型別引數Spring Boot型別
- http請求中get和post方法的區別HTTP
- 使用CloseableHttpClient 訪問 http 和https 的get請求HTTPclient
- java傳送get請求帶引數Java
- Java 用jetty實現HTTP伺服器 獲取 Get 請求體JavaJettyHTTP伺服器
- onethink 如何使用get請求?
- Node.js GET請求Node.js
- requests模組 - get 請求
- 傳送GET請求 示例
- 使用request庫的get方法發起GET請求
- 網路通訊5:執行HTTP的GET/POST請求HTTP
- 淺談HTTP中GET和POST請求方式的區別HTTP
- Http請求get與post請求方式的各種相關面試總結HTTP面試
- Get提交方式中文亂碼
- POST與GET請求區別
- python requests get請求 如何獲取所有請求Python
- vue 發起get請求和post請求Vue
- get請求和post請求的區別
- 請求OpenFeign的GET請求時,請求為何失敗?
- Golang:使用go-resty/resty傳送http請求get和postGolangRESTHTTP
- SpringMVC中如何傳送GET請求、POST請求、PUT請求、DELETE請求。SpringMVCdelete
- Java HttpClient 學習記錄 2 嘗試Get請求JavaHTTPclient
- Vue中通過Axios向SpringBoot傳送get和post請求VueiOSSpring Boot
- axios 發get,post 請求小結iOS
- get與post的請求區別
- python介面測試—get請求(一)Python
- go對get、post請求封裝Go封裝
- GET請求的引數丟失