Java利用HttpURLConnection发送post请求

本文介绍如何使用 Java 的 HttpURLConnection 类实现 HTTP POST 请求。通过设置请求属性、定义超时时间及构建请求参数,实现了与服务器的数据交互。
  1. URL url = null;  
  2. HttpURLConnection http = null;  
  3.   
  4. try {  
  5.     url = new URL(urls);  
  6.     http = (HttpURLConnection) url.openConnection();  
  7.     http.setDoInput(true);  
  8.     http.setDoOutput(true);  
  9.     http.setUseCaches(false);  
  10.     http.setConnectTimeout(50000);//设置连接超时  
  11. //如果在建立连接之前超时期满,则会引发一个 java.net.SocketTimeoutException。超时时间为零表示无穷大超时。  
  12.     http.setReadTimeout(50000);//设置读取超时  
  13. //如果在数据可读取之前超时期满,则会引发一个 java.net.SocketTimeoutException。超时时间为零表示无穷大超时。            
  14.     http.setRequestMethod("POST");  
  15.     // http.setRequestProperty("Content-Type","text/xml; charset=UTF-8");  
  16.     http.setRequestProperty("Content-Type""application/x-www-form-urlencoded");  
  17.     http.connect();  
  18.     param = "&appName=" + appName   
  19.             + "&token=" + token   
  20.             + "&method=" + method   
  21.             + "&dataType=" + dataType   
  22.             + "&dataParams=" + dataParams   
  23.             + "&sign=" + sign;  
  24.   
  25.     OutputStreamWriter osw = new OutputStreamWriter(http.getOutputStream(), "utf-8");  
  26.     osw.write(param);  
  27.     osw.flush();  
  28.     osw.close();  
  29.   
  30.     if (http.getResponseCode() == 200) {  
  31.         BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream(), "utf-8"));  
  32.         String inputLine;  
  33.         while ((inputLine = in.readLine()) != null) {  
  34.             result += inputLine;  
  35.         }  
  36.         in.close();  
  37.         //result = "["+result+"]";  
  38.     }  
  39. catch (Exception e) {  
  40.     System.out.println("err");  
  41. finally {  
  42.     if (http != null) http.disconnect();  
  43.     if (fis != null) fis.close();  
  44. }  
URL url = null;
HttpURLConnection http = null;

try {
	url = new URL(urls);
	http = (HttpURLConnection) url.openConnection();
	http.setDoInput(true);
	http.setDoOutput(true);
	http.setUseCaches(false);
	http.setConnectTimeout(50000);//设置连接超时
//如果在建立连接之前超时期满,则会引发一个 java.net.SocketTimeoutException。超时时间为零表示无穷大超时。
	http.setReadTimeout(50000);//设置读取超时
//如果在数据可读取之前超时期满,则会引发一个 java.net.SocketTimeoutException。超时时间为零表示无穷大超时。			
	http.setRequestMethod("POST");
	// http.setRequestProperty("Content-Type","text/xml; charset=UTF-8");
	http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
	http.connect();
	param = "&appName=" + appName 
			+ "&token=" + token 
			+ "&method=" + method 
			+ "&dataType=" + dataType 
			+ "&dataParams=" + dataParams 
			+ "&sign=" + sign;

	OutputStreamWriter osw = new OutputStreamWriter(http.getOutputStream(), "utf-8");
	osw.write(param);
	osw.flush();
	osw.close();

	if (http.getResponseCode() == 200) {
		BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream(), "utf-8"));
		String inputLine;
		while ((inputLine = in.readLine()) != null) {
			result += inputLine;
		}
		in.close();
		//result = "["+result+"]";
	}
} catch (Exception e) {
	System.out.println("err");
} finally {
	if (http != null) http.disconnect();
	if (fis != null) fis.close();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值