JAVA的net发送_通过java.net.URLConnection发送HTTP请求的方法

1 packagecn.smartercampus.cloud.core.util;2

3 importjava.io.BufferedReader;4 importjava.io.InputStream;5 importjava.io.InputStreamReader;6 importjava.io.OutputStream;7 importjava.io.OutputStreamWriter;8 importjava.net.HttpURLConnection;9 importjava.net.URL;10 importjava.net.URLConnection;11

12

13

14 public classmyHttpUtil {15

16 public static void setUserInfo() throwsException {17 doGet("http://localhost/api/setUserInfo_test");18 }19

20 public static void main(String[] args) throwsException {21 System.out.println(doGet("http://localhost/api/queryForList/prepare.getMyCollectBeike"));22 }23

24

25 /**

26 * Post Request27 *@return

28 *@throwsException29 */

30 public static String doPost(String url) throwsException {31 String parameterData = "test=test&2=2";32

33 URL localURL = newURL(url);34 URLConnection connection =localURL.openConnection();35 HttpURLConnection httpURLConnection =(HttpURLConnection)connection;36

37 httpURLConnection.setDoOutput(true);38 httpURLConnection.setRequestMethod("POST");39 httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");40 httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");41 httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterData.length()));42

43 OutputStream outputStream = null;44 OutputStreamWriter outputStreamWriter = null;45 InputStream inputStream = null;46 InputStreamReader inputStreamReader = null;47 BufferedReader reader = null;48 StringBuffer resultBuffer = newStringBuffer();49 String tempLine = null;50

51 try{52 outputStream =httpURLConnection.getOutputStream();53 outputStreamWriter = newOutputStreamWriter(outputStream);54

55 outputStreamWriter.write(parameterData.toString());56 outputStreamWriter.flush();57

58 if (httpURLConnection.getResponseCode() >= 300) {59 throw new Exception("HTTP Request is not success, Response code is " +httpURLConnection.getResponseCode());60 }61

62 inputStream =httpURLConnection.getInputStream();63 inputStreamReader = newInputStreamReader(inputStream);64 reader = newBufferedReader(inputStreamReader);65

66 while ((tempLine = reader.readLine()) != null) {67 resultBuffer.append(tempLine);68 }69

70 } finally{71

72 if (outputStreamWriter != null) {73 outputStreamWriter.close();74 }75

76 if (outputStream != null) {77 outputStream.close();78 }79

80 if (reader != null) {81 reader.close();82 }83

84 if (inputStreamReader != null) {85 inputStreamReader.close();86 }87

88 if (inputStream != null) {89 inputStream.close();90 }91

92 }93

94 returnresultBuffer.toString();95 }96

97

98 /**

99 * Get Request100 *@return

101 *@throwsException102 */

103 public static String doGet(String url) throwsException {104 URL localURL = newURL(url);105 URLConnection connection =localURL.openConnection();106 HttpURLConnection httpURLConnection =(HttpURLConnection)connection;107

108 httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");109 httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");110

111 InputStream inputStream = null;112 InputStreamReader inputStreamReader = null;113 BufferedReader reader = null;114 StringBuffer resultBuffer = newStringBuffer();115 String tempLine = null;116

117 if (httpURLConnection.getResponseCode() >= 300) {118 throw new Exception("HTTP Request is not success, Response code is " +httpURLConnection.getResponseCode());119 }120

121 try{122 inputStream =httpURLConnection.getInputStream();123 inputStreamReader = newInputStreamReader(inputStream);124 reader = newBufferedReader(inputStreamReader);125

126 while ((tempLine = reader.readLine()) != null) {127 resultBuffer.append(tempLine);128 }129

130 } finally{131

132 if (reader != null) {133 reader.close();134 }135

136 if (inputStreamReader != null) {137 inputStreamReader.close();138 }139

140 if (inputStream != null) {141 inputStream.close();142 }143

144 }145

146 returnresultBuffer.toString();147 }148

149 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值