100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > HttpUtil请求工具类

HttpUtil请求工具类

时间:2023-03-01 10:59:06

相关推荐

HttpUtil请求工具类

Http工具类post和get请求都有

import com.aisino.mon.logs.Logger;import mons.httpclient.HttpClient;import mons.httpclient.SimpleHttpConnectionManager;import mons.httpclient.methods.GetMethod;import mons.httpclient.methods.PostMethod;import mons.httpclient.params.HttpMethodParams;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import .URL;import .URLConnection;import java.util.Map;public class HttpUtil {private static Logger log = Logger.getLogger("httputil_log");private final static int CONNECT_TIMEOUT = 5000; // in millisecondsprivate final static String DEFAULT_ENCODING = "UTF-8";public static String postData(String urlStr, String data) {return postData(urlStr, data, null);}public static String postData(String urlStr, String data, String contentType) {BufferedReader reader = null;try {URL url = new URL(urlStr);URLConnection conn = url.openConnection();conn.setDoOutput(true);conn.setConnectTimeout(CONNECT_TIMEOUT);conn.setReadTimeout(CONNECT_TIMEOUT);if (contentType != null)conn.setRequestProperty("content-type", contentType);OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);if (data == null) {writer.write("");} else {writer.write(data);}writer.flush();writer.close();reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));StringBuilder sb = new StringBuilder();String line = null;while ((line = reader.readLine()) != null) {sb.append(line);sb.append("\r\n");}return sb.toString();} catch (IOException e) {e.printStackTrace();log.error(e);} finally {try {if (reader != null)reader.close();} catch (IOException e) {log.error(e);}}return null;}public static String postHttp(String url, Map<String, String> map) {HttpClient httpclient = null;PostMethod post = null;SimpleHttpConnectionManager simpleHttpConnectionManager = null;String info = null;try {httpclient = new HttpClient();post = new PostMethod(url);// 设置编码方式post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);httpclient.getHttpConnectionManager().getParams().setSoTimeout(30000);// 添加参数if (map != null) {for (Map.Entry<String, String> entry : map.entrySet()) {String key = entry.getKey();post.addParameter(key, entry.getValue());}}// 执行httpclient.executeMethod(post);// 接口返回信息info = new String(post.getResponseBody(), "UTF-8");} catch (Exception e) {log.error(e);} finally {// 关闭连接,释放资源if (post != null) {post.releaseConnection();}if (httpclient != null) {simpleHttpConnectionManager = ((SimpleHttpConnectionManager) httpclient.getHttpConnectionManager());if (simpleHttpConnectionManager != null) {simpleHttpConnectionManager.shutdown();}}}return info;}/*** 处理get请求* * @param url* 请求地址 如* http://localhost:9090/base_rpc/basicData/getInvoice?t=1507513445960&invoiceId=039D906D07C74306B635DD89F87584CD&token=bd302857fbd4a01af7401fe229d43918* @return*/public static String getHttp(String url) {HttpClient httpClient = null;GetMethod get = null;SimpleHttpConnectionManager simpleHttpConnectionManager = null;String info = null;try {httpClient = new HttpClient();get = new GetMethod(url);// 执行httpClient.executeMethod(get);// 接口返回信息info = new String(get.getResponseBody(), "UTF-8");log.info("接口【" + url + "】 返回:" + info);} catch (Exception e) {log.info("调用接口【" + url + "】 出错:" + e);return null;} finally {if (get != null) {get.releaseConnection();}if (httpClient != null) {simpleHttpConnectionManager = (SimpleHttpConnectionManager) httpClient.getHttpConnectionManager();if (simpleHttpConnectionManager != null) {simpleHttpConnectionManager.shutdown();}}}return info;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。