public static void main(String[] args) throws Exception{
String urlPath = new String("http://localhost:8080/Test1/HelloWorld");
//String urlPath = new String("http://localhost:8080/Test1/HelloWorld?name=丁丁".getBytes("UTF-8"));
String param="name="+URLEncoder.encode("丁丁","UTF-8");
//建立连接
URL url=new URL(urlPath);
HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
//设置参数
httpConn.setDoOutput(true); //需要输出
httpConn.setDoInput(true); //需要输入
httpConn.setUseCaches(false); //不允许缓存
httpConn.setRequestMethod("POST"); //设置POST方式连接
//设置请求属性
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
httpConn.setRequestProperty("Charset", "UTF-8");
//连接,也可以不用明文connectÿ
JAVA使用原始HttpURLConnection发送POST数据
最新推荐文章于 2024-02-28 10:46:41 发布