Http请求工具类
来源:互联网 发布:廊坊新奥集团招聘 java 编辑:程序博客网 时间:2024/05/18 17:00
import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.Map;/** * HTTP请求链接工具类 * @author zhangpengliang * @version 2017-09-27 * */public class HttpConnectUtil {/*** * 判断接受地址是否连接 * * @param urlPath * @return boolean */public static boolean isSuccessConnected(String urlPath){URL url;try {url = new URL(urlPath);HttpURLConnection connection = (HttpURLConnection)url.openConnection();connection.setDoInput(true);connection.setDoOutput(true);connection.setRequestMethod("POST");connection.setUseCaches(false);connection.setConnectTimeout(4000);connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");connection.connect();int responseCode = connection.getResponseCode();if(responseCode == 200){return true;}} catch (Exception e) {e.printStackTrace();}return false;}/** * POST 发送请求 * @param urlPath 链接地址URL * @param xml 报文结构xml * @return */public static String post(String urlPath, String xml){OutputStreamWriter out = null;if(StringUtil.isEmpty(urlPath)){return null;//需要抛出一个错误}try {URL url = new URL(urlPath);HttpURLConnection connection = (HttpURLConnection)url.openConnection();connection.setDoInput(true);connection.setDoOutput(true);connection.setRequestMethod("POST");connection.setUseCaches(false);connection.setConnectTimeout(4000);//下面就是添加头信息connection.setRequestProperty("Content-Type","text/xml");connection.setRequestProperty("charset", "UTF-8");connection.connect();out = new OutputStreamWriter(connection.getOutputStream());out.write(xml);out.flush();out.close(); //读取响应InputStream inputStream = connection.getInputStream();//真正发送请求 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes()); sb.append(lines); } System.out.println(sb.toString()); reader.close(); // 断开连接 connection.disconnect();return sb.toString();}catch(MalformedURLException ee){System.out.println(ee);}catch (FileNotFoundException fileNotFoundException) {fileNotFoundException.printStackTrace();}catch (Exception e) {e.printStackTrace();}finally{if(out != null){try {out.close();} catch (IOException e) {e.printStackTrace();}}}return null;}/** * POST 发送请求 * @param urlPath 链接地址 * @param xml 报文结构xml * @param param 头部信息参数 * @return */public static String post(String urlPath, String xml,Map<String, Object> param){OutputStreamWriter out = null;if(StringUtil.isEmpty(urlPath)){return null;//需要抛出一个错误}try {URL url = new URL(urlPath);HttpURLConnection connection = (HttpURLConnection)url.openConnection();connection.setDoInput(true);connection.setDoOutput(true);connection.setRequestMethod("POST");connection.setUseCaches(false);connection.setConnectTimeout(4000);//下面就是添加头信息connection.setRequestProperty("Content-Type","text/xml");connection.setRequestProperty("charset", "UTF-8");for(String s:param.keySet()){connection.setRequestProperty(s, param.get(s).toString());}connection.connect();out = new OutputStreamWriter(connection.getOutputStream());out.write(xml);out.flush();out.close(); //读取响应InputStream inputStream = connection.getInputStream();//真正发送请求 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes()); sb.append(lines); } System.out.println(sb.toString()); reader.close(); // 断开连接 connection.disconnect();return sb.toString();}catch(MalformedURLException ee){System.out.println(ee);}catch (FileNotFoundException fileNotFoundException) {fileNotFoundException.printStackTrace();}catch (Exception e) {e.printStackTrace();}finally{if(out != null){try {out.close();} catch (IOException e) {e.printStackTrace();}}}return null;}}
阅读全文
0 0
- Http请求工具类
- http请求工具类
- http请求工具类
- HTTP 请求工具类
- http请求工具类
- HTTP请求工具类
- HTTP请求工具类
- http请求工具类
- http请求工具类
- 工具类【Http请求】
- Http请求工具类
- HTTP 请求 工具类
- Http请求工具类
- Http请求工具类
- HTTP请求工具类
- Iphone HTTP请求工具类
- Android----http请求工具类
- HTTP请求工具类(Java)
- MTK Android内核配置makemenuconfig
- 良心GitHub项目:各种机器学习任务的顶级结果(论文)汇总
- “挖空三座山、装了几万台服务器”的绿色数据中心
- Dubbo+Zookeeper+Spring 分布式服务的搭建(一)
- 昨天学习的基础进行复习
- Http请求工具类
- Js引用资源路径问题
- CSDN 博客 修改文章搜索为 bing 搜索,且只搜索自己的博客的方法
- spring4 mvc + poi 导出excel
- atos命令符化号crash文件
- JAVA WEB 实现文件数据导入(CSV、TXT、excel)
- 定时获取微信access_token
- angular基础指令
- 微信Mac版下载