1.http发送 post请求
1.1 JSONObject转换为String类型后进行发送
1.1.1头部请求添加:` conn.setRequestProperty(“Content-Type”, “application/json”);
`文章来源:https://www.toymoban.com/news/detail-679047.html
1.1.2 发送操作主要部分
OutputStreamWriter out = null;
String json = obj.toJSONString();
out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
out.write(json);
out.flush();
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param obj 请求参数,请求参数应该是JSONObject 的形式。
* @return 所代表远程资源的响应结果
*/
public static String dataPost(String url, JSONObject obj)
{
OutputStreamWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try
{
String urlNameString = url;
log.info("sendPost - {}", urlNameString);
URL realUrl = new URL(urlNameString);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setDoOutput(true);
conn.setDoInput(true);
String json = obj.toJSONString();
//发送post请求
out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
out.write(json);
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null)
{
result.append(line);
}
log.info("recv - {}", result);
}
catch (ConnectException e)
{
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",obj=" + obj, e);
}
catch (SocketTimeoutException e)
{
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",obj=" + obj, e);
}
catch (IOException e)
{
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",obj=" + obj, e);
}
catch (Exception e)
{
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",obj=" + obj, e);
}
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}
catch (IOException ex)
{
log.error("调用in.close Exception, url=" + url + ",obj=" + obj, ex);
}
}
return result.toString();
}
2.https发送 post请求
2.1 JSONObject转换为String类型后进行发送
2.1.1头部请求添加:` conn.setRequestProperty(“Content-Type”, “application/json”);
`文章来源地址https://www.toymoban.com/news/detail-679047.html
2.1.2 发送操作主要部分
OutputStreamWriter out = null;
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
String json = obj.toJSONString();
out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
out.write(json);
out.flush();
/**
* @param url 发送请求的 URL
* @param obj 请求参数,请求参数应该是JSONObject 的形式。
* @return 所代表远程资源的响应结果
* */
public static String dataPostSSl(String url, JSONObject obj) {
OutputStreamWriter out = null;
StringBuilder result = new StringBuilder();
String urlNameString = url;
try
{
log.info("sendSSLPost - {}", urlNameString);
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
URL console = new URL(urlNameString);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.connect();
String json = obj.toJSONString();
//发送post请求
out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
out.write(json);
out.flush();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String ret = "";
while ((ret = br.readLine()) != null)
{
if (ret != null && !"".equals(ret.trim()))
{
result.append(new String(ret.getBytes("ISO-8859-1"), "utf-8"));
}
}
log.info("recv - {}", result);
conn.disconnect();
br.close();
}
catch (ConnectException e)
{
log.error("调用HttpUtils.sendSSLPost ConnectException, url=" + url + ",url=" + url, e);
}
catch (SocketTimeoutException e)
{
log.error("调用HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",url=" + url, e);
}
catch (IOException e)
{
log.error("调用HttpUtils.sendSSLPost IOException, url=" + url + ",url=" + url, e);
}
catch (Exception e)
{
log.error("调用HttpsUtil.sendSSLPost Exception, url=" + url + ",url=" + url, e);
}
return result.toString();
}
到了这里,关于ruoyi 后端发送http/https post请求的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!