java 发送http请求post get
来源:互联网 发布:淘宝会员等级 编辑:程序博客网 时间:2024/04/19 13:58
package com.wcg.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.net.URL;import java.net.URLConnection;import java.net.URLEncoder;public class HttpUtil {/** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return URL 所代表远程资源的响应结果 */public static String sendGet(String url, String param) {String result = "";BufferedReader in = null;try {String urlNameString = url + "?" + param;URL realUrl = new URL(urlNameString);// 打开和URL之间的连接URLConnection connection = realUrl.openConnection();// 设置通用的请求属性connection.setConnectTimeout(60000);// 建立实际的连接connection.connect();// 定义 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();// 发送POST请求必须设置如下两行conn.setDoOutput(true);conn.setDoInput(true);conn.setConnectTimeout(60000);// 获取URLConnection对象对应的输出流out = new PrintWriter(conn.getOutputStream());// 发送请求参数out.print(param);// 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 getUrlDecode(String str) {String res = null;try {res = URLEncoder.encode(str, "utf-8");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}return res;}public static void main(String[] args) {//String partnerKey = "123123";// 合作平台编号4String uid = "1040";// 合作平台订单号3String orderid = "201406191527326";// 充值账号1String account_val = "626659";// 2String companyid = "148";//String enshrin = "account_val" + "626659" + "companyid" + "148"+ "orderid" + "201406191527326" + "uid" + "1040" + partnerKey;String sign = MD5.GetMD5Code(enshrin);String param = "account_val=" + getUrlDecode(account_val)+ "&companyid=" + getUrlDecode(companyid) + "&orderid="+ getUrlDecode(orderid) + "&uid=" + getUrlDecode(uid)+ "&sign=" + sign;// 发送 GET 请求System.out.println(param);String s = HttpUtil.sendGet(PropertiesUtil.getArrearsUrl(), param);System.out.println(s);String enshrin2 = "money" + "50" + "orderid" + "201406191527326" + "uid" + "1040" + partnerKey;String sign2 = MD5.GetMD5Code(enshrin2);String param2 ="money=50&orderid=201406191527326&uid=1040&sign="+sign2;System.out.println(param2);String s2 = HttpUtil.sendGet(PropertiesUtil.getTransUrl(), param2);System.out.println(s2);}}
0 0
- JAVA 发送Http请求,GET、POST请求
- java发送HTTP GET POST请求
- java发送get、post http请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java 发送http请求post get
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- [Android Bug] ListView中Header, Footer无法隐藏(gone)的问题
- JavaScript基本信息
- MFC中用ActiveX控件实现串口通信
- PHP5 session 详解【经典】
- iTunes Connect 开发者上手经验
- java 发送http请求post get
- 跑CTS,GTS
- 如何实现可动态调整隐藏header的listview
- [网络通信]CocoaAsyncSocket的使用
- 其他BLOG链接
- 英格兰两连败还能出线
- HandlerInterceptorAdapter
- [JS]禁用浏览器退格键
- 黑马程序员java基础集合知识整合