package com.neusoft.unieap; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public class TestPost { /** * 向指定 URL 发送get方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 [name1=value1,name2=value2] 的形式。 * @return 所代表远程资源的响应结果 */ public static String sendGet(String url, String [] param) { String result = ""; BufferedReader in = null; try { String paramVal = ""; for (int i = 0; i < param.length; i++) { paramVal += param[i] + "&"; } paramVal = paramVal.substring(0, paramVal.length()-1); String urlNameString = url + "?" + paramVal; URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 [name1=value1,name2=value2] 的形式。 * @return 所代表远程资源的响应结果 */ public static String sendPost(String url, String [] param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); String paramVal = ""; for (int i = 0; i < param.length; i++) { paramVal += param[i] + "&"; } paramVal = paramVal.substring(0, paramVal.length()-1); // 发送请求参数 out.print(paramVal); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送 POST 请求出现异常!"+e); e.printStackTrace(); } //使用finally块来关闭输出流、输入流 finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; } public static String sendPostToJson(String linkurl, String jsonString) { String resultStr = ""; try { // 创建url资源 URL urlv = new URL(linkurl); // 建立http连接 HttpURLConnection conn = (HttpURLConnection) urlv.openConnection(); // 设置允许输出 conn.setDoOutput(true); conn.setDoInput(true); // 设置不用缓存 conn.setUseCaches(false); // 设置传递方式 conn.setRequestMethod("POST"); // 设置维持长连接 conn.addRequestProperty("Connection", "Keep-Alive"); // 设置文件字符集: //conn.setRequestProperty("Charset", "UTF-8"); conn.addRequestProperty("Accept-Language", "zh-CN,zh;q=0.8"); conn.addRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.8 Safari/537.36"); //转换为字节数组 byte[] data = jsonString.getBytes("UTF-8"); // 设置文件长度 conn.addRequestProperty("Content-Length", String.valueOf(data.length)); // 设置文件类型: conn.addRequestProperty("Content-Type", "application/json; charset=UTF-8"); //String strJson = new String(jsonString.getBytes("GBK"),"UTF-8"); // 开始连接请求 conn.connect(); OutputStream out = conn.getOutputStream(); // 写入请求的字符串 out.write(data); out.flush(); out.close(); System.out.println(conn.getResponseCode()); // 请求返回的状态 if (conn.getResponseCode() == 200) { System.out.println("连接成功"); // 请求返回的数据 InputStream in = conn.getInputStream(); try { byte[] data1 = new byte[in.available()]; in.read(data1); // 转成字符串 resultStr = new String(data1); System.out.println(resultStr); } catch (Exception e1) { e1.printStackTrace(); } } else { System.out.println("连接异常"); } conn.disconnect(); } catch (Exception e) { resultStr = e.getMessage(); } return resultStr; } public static String sendPostToJsonNO(String linkurl) { String resultStr = ""; try { // 创建url资源 URL urlv = new URL(linkurl); // 建立http连接 HttpURLConnection conn = (HttpURLConnection) urlv.openConnection(); // 设置允许输出 conn.setDoOutput(true); conn.setDoInput(true); // 设置不用缓存 conn.setUseCaches(false); // 设置传递方式 conn.setRequestMethod("POST"); // 设置维持长连接 conn.addRequestProperty("Connection", "Keep-Alive"); // 设置文件字符集: //conn.setRequestProperty("Charset", "UTF-8"); conn.addRequestProperty("Accept-Language", "zh-CN,zh;q=0.8"); conn.addRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.8 Safari/537.36"); //转换为字节数组 // 设置文件长度 // 设置文件类型: conn.addRequestProperty("Content-Type", "application/json; charset=UTF-8"); //String strJson = new String(jsonString.getBytes("GBK"),"UTF-8"); // 开始连接请求 conn.connect(); OutputStream out = conn.getOutputStream(); // 写入请求的字符串 out.flush(); out.close(); System.out.println(conn.getResponseCode()); // 请求返回的状态 System.out.println("连接成功"); // 请求返回的数据 InputStream in = conn.getInputStream(); try { byte[] data1 = new byte[in.available()]; in.read(data1); // 转成字符串 resultStr = new String(data1); System.out.println(resultStr); } catch (Exception e1) { e1.printStackTrace(); } conn.disconnect(); } catch (Exception e) { resultStr = e.getMessage(); } return resultStr; } /** * 向指定 URL 发送get方法的请求 * * @param url * 发送请求的 URL * @param * @return 所代表远程资源的响应结果 */ public static String sendGet(String url) { String result = ""; BufferedReader in = null; try { /*String paramVal = ""; for (int i = 0; i < param.length; i++) { paramVal += param[i] + "&"; } paramVal = paramVal.substring(0, paramVal.length()-1); String urlNameString = url + "?" + paramVal;*/ URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } }
java http post请求测试
最新推荐文章于 2024-07-30 16:45:29 发布