java项目 之间通讯_两个java项目之间的通讯

1 packagecom.util;2

3 importjava.io.BufferedReader;4 importjava.io.IOException;5 importjava.io.InputStream;6 importjava.io.InputStreamReader;7 importjava.io.OutputStream;8 importjava.io.OutputStreamWriter;9 importjava.net.HttpURLConnection;10 importjava.net.MalformedURLException;11 importjava.net.URL;12 importjava.net.URLConnection;13 importjava.util.Iterator;14 importjava.util.Map;15

16 public classHttpConnectionUtil {17

18 //post请求

19 public static final String HTTP_POST = "POST";20

21 //get请求

22 public static final String HTTP_GET = "GET";23

24 //utf-8字符编码

25 public static final String CHARSET_UTF_8 = "utf-8";26

27 //HTTP内容类型。如果未指定ContentType,默认为TEXT/HTML

28 public static final String CONTENT_TYPE_TEXT_HTML = "text/xml";29

30 //HTTP内容类型。相当于form表单的形式,提交暑假

31 public static final String CONTENT_TYPE_FORM_URL = "application/x-www-form-urlencoded";32

33 //请求超时时间

34 public static final int SEND_REQUEST_TIME_OUT = 50000;35

36 //将读超时时间

37 public static final int READ_TIME_OUT = 50000;38

39 /**

40 *41 *@paramrequestType42 * 请求类型43 *@paramurlStr44 * 请求地址45 *@parambody46 * 请求发送内容47 *@return返回内容48 */

49 public staticString requestMethod(String requestType, String urlStr, String body) {50

51 //是否有http正文提交

52 boolean isDoInput = false;53 if (body != null && body.length() > 0)54 isDoInput = true;55 OutputStream outputStream = null;56 OutputStreamWriter outputStreamWriter = null;57 InputStream inputStream = null;58 InputStreamReader inputStreamReader = null;59 BufferedReader reader = null;60 StringBuffer resultBuffer = newStringBuffer();61 String tempLine = null;62 try{63 //统一资源

64 URL url = newURL(urlStr);65 //连接类的父类,抽象类

66 URLConnection urlConnection =url.openConnection();67 //http的连接类

68 HttpURLConnection httpURLConnection =(HttpURLConnection) urlConnection;69

70 //设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在71 //http正文内,因此需要设为true, 默认情况下是false;

72 if(isDoInput) {73 httpURLConnection.setDoOutput(true);74 httpURLConnection.setRequestProperty("Content-Length", String.valueOf(body.length()));75 }76 //设置是否从httpUrlConnection读入,默认情况下是true;

77 httpURLConnection.setDoInput(true);78 //设置一个指定的超时值(以毫秒为单位)

79 httpURLConnection.setConnectTimeout(SEND_REQUEST_TIME_OUT);80 //将读超时设置为指定的超时,以毫秒为单位。

81 httpURLConnection.setReadTimeout(READ_TIME_OUT);82 //Post 请求不能使用缓存

83 httpURLConnection.setUseCaches(false);84 //设置字符编码

85 httpURLConnection.setRequestProperty("Accept-Charset", CHARSET_UTF_8);86 //设置内容类型

87 httpURLConnection.setRequestProperty("Content-Type", CONTENT_TYPE_FORM_URL);88 //设定请求的方法,默认是GET

89 httpURLConnection.setRequestMethod(requestType);90

91 //打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。92 //如果在已打开连接(此时 connected 字段的值为 true)的情况下调用 connect 方法,则忽略该调用。

93 httpURLConnection.connect();94

95 if(isDoInput) {96 outputStream =httpURLConnection.getOutputStream();97 outputStreamWriter = newOutputStreamWriter(outputStream);98 outputStreamWriter.write(body);99 outputStreamWriter.flush();//刷新

100 }101 if (httpURLConnection.getResponseCode() >= 300) {102 throw newException(103 "HTTP Request is not success, Response code is " +httpURLConnection.getResponseCode());104 }105

106 if (httpURLConnection.getResponseCode() ==HttpURLConnection.HTTP_OK) {107 inputStream =httpURLConnection.getInputStream();108 inputStreamReader = newInputStreamReader(inputStream);109 reader = newBufferedReader(inputStreamReader);110

111 while ((tempLine = reader.readLine()) != null) {112 resultBuffer.append(tempLine);113 resultBuffer.append("\n");114 }115 }116

117 } catch(MalformedURLException e) {118 e.printStackTrace();119 } catch(IOException e) {120 e.printStackTrace();121 } catch(Exception e) {122 e.printStackTrace();123 } finally {//关闭流

124

125 try{126 if (outputStreamWriter != null) {127 outputStreamWriter.close();128 }129 } catch(Exception e) {130 e.printStackTrace();131 }132 try{133 if (outputStream != null) {134 outputStream.close();135 }136 } catch(Exception e) {137 e.printStackTrace();138 }139 try{140 if (reader != null) {141 reader.close();142 }143 } catch(Exception e) {144 e.printStackTrace();145 }146 try{147 if (inputStreamReader != null) {148 inputStreamReader.close();149 }150 } catch(Exception e) {151 e.printStackTrace();152 }153 try{154 if (inputStream != null) {155 inputStream.close();156 }157 } catch(Exception e) {158 e.printStackTrace();159 }160 }161 returnresultBuffer.toString();162 }163

164 /**

165 * 将map集合的键值对转化成:key1=value1&key2=value2 的形式166 *167 *@paramparameterMap168 * 需要转化的键值对集合169 *@return字符串170 */

171 public staticString convertStringParamter(Map parameterMap) {172 StringBuffer parameterBuffer = newStringBuffer();173 if (parameterMap != null) {174 Iterator iterator =parameterMap.keySet().iterator();175 String key = null;176 String value = null;177 while(iterator.hasNext()) {178 key =(String) iterator.next();179 if (parameterMap.get(key) != null) {180 value =(String) parameterMap.get(key);181 } else{182 value = "";183 }184 parameterBuffer.append(key).append("=").append(value);185 if(iterator.hasNext()) {186 parameterBuffer.append("&");187 }188 }189 }190 returnparameterBuffer.toString();191 }192

193 public static void main(String[] args) throwsMalformedURLException {194

195 System.out.println(requestMethod(HTTP_GET, "http://127.0.0.1:8080/TestHttpRequestServlet",196 "username=123&password=我是谁"));197

198 }199 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值