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
- java傳送http的get、post請求JavaHTTP
- HTTP Get,Post請求詳解HTTP
- GET請求引數為中文時亂碼分析
- Android Http請求框架一:Get 和 Post 請求AndroidHTTP框架
- Python中get、post請求詳解(HTTP請求頭、狀態碼)PythonHTTP
- jboss get請求中文亂碼問題的解決[zt]
- 【轉】怎麼用PHP傳送HTTP請求(POST請求、GET請求)?PHPHTTP
- http請求之get和post的區別HTTP
- Go使用net/http庫傳送GET請求GoHTTP
- Go HTTP GET 請求可以傳送 body 嗎GoHTTP
- java傳送GET和post請求Java
- Postman 使用 Get 請求 URL 傳參中文亂碼的問題Postman
- Servlet中request請求Get和Post方法以及亂碼解決Servlet
- requests模組 - get 請求
- http請求中get和post方法的區別HTTP
- HTTP協議GET和POST請求的區別HTTP協議
- HTTP協議中請求方法的Get和PostHTTP協議
- java傳送get請求帶引數Java
- Java 用jetty實現HTTP伺服器 獲取 Get 請求體JavaJettyHTTP伺服器
- 傳送GET請求 示例
- XHR物件的get請求物件
- Node.js GET請求Node.js
- 使用CloseableHttpClient 訪問 http 和https 的get請求HTTPclient
- SpringBoot Get 請求接收 Date 型別引數Spring Boot型別
- 使用request庫的get方法發起GET請求
- iOS 同步請求 非同步請求 GET請求 POST請求iOS非同步
- JAVA中Get和Post請求的區別Java
- Http請求get與post請求方式的各種相關面試總結HTTP面試
- 網路通訊5:執行HTTP的GET/POST請求HTTP
- C# 之HTTP請求get,post帶重試引數C#HTTP
- 淺談HTTP中GET和POST請求方式的區別HTTP
- onethink 如何使用get請求?
- POST與GET請求區別
- 使用HttpClient傳送GET請求HTTPclient
- Get和Post請求詳解
- Get提交方式中文亂碼