java公众平台通用接口工具类HttpConnectUtil实例代码
程序员文章站
2023-12-19 19:42:40
实例如下:
package com.common.util;
import java.io.bufferedreader;
import java.io....
实例如下:
package com.common.util; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.net.connectexception; import java.net.httpurlconnection; import java.net.url; import java.util.date; import javax.net.ssl.httpsurlconnection; import javax.net.ssl.sslcontext; import javax.net.ssl.sslsocketfactory; import javax.net.ssl.trustmanager; import javax.servlet.http.httpservletrequest; import org.slf4j.logger; import org.slf4j.loggerfactory; import com.common.weixin.access.util.myx509trustmanager; /** * 公众平台通用接口工具类 * * @author * @date 2013-08-09 */ public class httpconnectutil { private static logger log = loggerfactory.getlogger(httpconnectutil.class); /** * 发起https请求并获取结果 * * @param requesturl 请求地址 * @param requestmethod 请求方式(get、post) * @param outputstr 提交的数据 * @return jsonobject(通过jsonobject.get(key)的方式获取json对象的属性值) */ public static string httprequest(string requesturl, string requestmethod, string outputstr) { string result = null; stringbuffer buffer = new stringbuffer(); try { url url = new url(requesturl); httpurlconnection httpurlconn = (httpurlconnection) url.openconnection(); // 设置通用的请求属性 httpurlconn.setrequestproperty("accept", "*/*"); httpurlconn.setrequestproperty("connection", "keep-alive"); httpurlconn.setrequestproperty("charset", "utf-8"); httpurlconn.setdooutput(true); httpurlconn.setdoinput(true); httpurlconn.setusecaches(false); // 设置请求方式(get/post) httpurlconn.setrequestmethod(requestmethod); if ("get".equalsignorecase(requestmethod)) httpurlconn.connect(); // 当有数据需要提交时 if (null != outputstr) { outputstream outputstream = httpurlconn.getoutputstream(); // 注意编码格式,防止中文乱码 outputstream.write(outputstr.getbytes("utf-8")); outputstream.close(); } // 将返回的输入流转换成字符串 inputstream inputstream = httpurlconn.getinputstream(); inputstreamreader inputstreamreader = new inputstreamreader(inputstream, "utf-8"); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); string str = null; while ((str = bufferedreader.readline()) != null) { buffer.append(str); } bufferedreader.close(); inputstreamreader.close(); // 释放资源 inputstream.close(); inputstream = null; httpurlconn.disconnect(); result = buffer.tostring(); //jsonobject = jsonobject.fromobject(buffer.tostring()); } catch (connectexception ce) { log.error("weixin server connection timed out."); } catch (exception e) { log.error("https request error:{}", e); } return result; } /** * 发起https请求并获取结果 * * @param requesturl 请求地址 * @param requestmethod 请求方式(get、post) * @param outputstr 提交的数据 * @return 结果字符串 */ public static string httpsrequest(string requesturl, string requestmethod, string outputstr) throws exception{ string result = null; stringbuffer buffer = new stringbuffer(); try { // 创建sslcontext对象,并使用我们指定的信任管理器初始化 trustmanager[] tm = { new myx509trustmanager() }; sslcontext sslcontext = sslcontext.getinstance("ssl", "sunjsse"); sslcontext.init(null, tm, new java.security.securerandom()); // 从上述sslcontext对象中得到sslsocketfactory对象 sslsocketfactory ssf = sslcontext.getsocketfactory(); url url = new url(requesturl); httpsurlconnection httpurlconn = (httpsurlconnection) url.openconnection(); httpurlconn.setsslsocketfactory(ssf); // 设置通用的请求属性 httpurlconn.setrequestproperty("accept", "*/*"); httpurlconn.setrequestproperty("connection", "keep-alive"); httpurlconn.setrequestproperty("charset", "utf-8"); httpurlconn.setdooutput(true); httpurlconn.setdoinput(true); httpurlconn.setusecaches(false); // 设置请求方式(get/post) httpurlconn.setrequestmethod(requestmethod); if ("get".equalsignorecase(requestmethod)) httpurlconn.connect(); // 当有数据需要提交时 if (null != outputstr) { outputstream outputstream = httpurlconn.getoutputstream(); // 注意编码格式,防止中文乱码 outputstream.write(outputstr.getbytes("utf-8")); outputstream.close(); } // 将返回的输入流转换成字符串 inputstream inputstream = httpurlconn.getinputstream(); inputstreamreader inputstreamreader = new inputstreamreader(inputstream, "utf-8"); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); string str = null; while ((str = bufferedreader.readline()) != null) { buffer.append(str); } bufferedreader.close(); inputstreamreader.close(); // 释放资源 inputstream.close(); inputstream = null; httpurlconn.disconnect(); result = buffer.tostring(); //jsonobject = jsonobject.fromobject(buffer.tostring()); } catch (connectexception ce) { log.error(requesturl + " server connection timed out."); throw new exception("连接服务器超时"); } catch (exception e) { log.error(requesturl +" https request error:{}", e); throw new exception("https请求错误"); } return result; } public static string getipaddr(httpservletrequest request) { string ip = request.getheader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip)) { ip = request.getheader("proxy-client-ip"); } if (ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip)) { ip = request.getheader("wl-proxy-client-ip"); } if (ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip)) { ip = request.getremoteaddr(); } return ip; } }
以上这篇java公众平台通用接口工具类httpconnectutil实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。