package com.kingbal.king.dmp;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import javax.net.ssl.HttpsURLConnection;
import java.io.OutputStream;
import java.net.URL;
/**
* <b>Function: </b> todo
*
* @program: http请求
* @Package: com.kingbal.king.dmp
* @author: songjianlin
* @date: 2024/09/03
* @version: 1.0
* @Copyright: 2024 www.kingbal.com Inc. All rights reserved.
*/
@Slf4j
public class BaseTest {
public static void main(String[] args) throws Exception {
String httpsURL = "https://www.kingbal.com";
URL myURL = new URL(httpsURL);
HttpsURLConnection conn = (HttpsURLConnection) myURL.openConnection();
conn.setRequestProperty("Connection","keep-alive");
conn.setRequestProperty("charset", "UTF-8");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36");
JSONObject json = JSONUtil.createObj();
json.putOpt("tst", 1);
// 发送GET请求
conn.setRequestMethod("POST");
OutputStream os = conn.getOutputStream();
os.write(json.toString().getBytes("UTF-8"));
os.close();
// 获取服务器响应码
int responseCode = conn.getResponseCode();
System.out.println(responseCode);
// 关闭连接
conn.disconnect();
}
}
我们在请求一个地址的时候带有参数结果报错:
Exception in thread "main" java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1432)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1421)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:220)
at com.kingbal.king.dmp.BaseTest.main(BaseTest.java:44)
这个报错表示正在尝试通过一个URLConnection对象写入数据,但是这个连接的doOutput属性被设置为了false。在Java网络编程中,URLConnection的doOutput属性决定了是否此连接应该被用于输出数据。
如果要处理此问题:
将doOutput属性设置为true。
package com.kingbal.king.dmp;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import javax.net.ssl.HttpsURLConnection;
import java.io.OutputStream;
import java.net.URL;
/**
* <b>Function: </b> todo
*
* @program: http请求
* @Package: com.kingbal.king.dmp
* @author: songjianlin
* @date: 2024/09/03
* @version: 1.0
* @Copyright: 2024 www.kingbal.com Inc. All rights reserved.
*/
@Slf4j
public class BaseTest {
public static void main(String[] args) throws Exception {
String httpsURL = "https://www.kingbal.com";
URL myURL = new URL(httpsURL);
HttpsURLConnection conn = (HttpsURLConnection) myURL.openConnection();
conn.setDoOutput(true); //上传参数,需要打开output流,将参数写到流中提交
conn.setRequestProperty("Connection","keep-alive");
conn.setRequestProperty("charset", "UTF-8");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36");
JSONObject json = JSONUtil.createObj();
json.putOpt("tst", 1);
// 发送GET请求
conn.setRequestMethod("POST");
OutputStream os = conn.getOutputStream();
os.write(json.toString().getBytes("UTF-8"));
os.close();
// 获取服务器响应码
int responseCode = conn.getResponseCode();
System.out.println(responseCode);
// 关闭连接
conn.disconnect();
}
}
修改为上述代码即可正常访问~~~~