HttpSenderUtil向指定 URL 发送POST方法的请求
package com.founder.ec.common.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.Date; import java.util.Map; import java.util.Set; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.URIException; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.util.URIUtil; import org.apache.commons.lang.StringUtils; import org.apache.commons.httpclient.HttpException; public class HttpSenderUtil { /** * 向指定 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()); // 发送请求参数 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 httpSend(String url, Map<String, Object> propsMap) throws Exception{ HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url);// POST请求 String returnString =""; // 参数设置 Set<String> keySet = propsMap.keySet(); NameValuePair[] postData = new NameValuePair[keySet.size()]; int index = 0; for (String key : keySet) { postData[index++] = new NameValuePair(key, propsMap.get(key).toString()); } postMethod.addParameters(postData); try { httpClient.executeMethod(postMethod);// 发送请求 java.io.InputStream input = postMethod.getResponseBodyAsStream(); returnString = convertStreamToString(input).toString(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { postMethod.releaseConnection();// 关闭连接 } return returnString; } public static String convertStreamToString(java.io.InputStream input) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } input.close(); return sb.toString(); } public static void main(String[] strs){ long time=new Date().getTime(); String check=MD5.getMD5(time+"www.j1.com"); String mobile="13053702096"; String msg="尊敬的用户 , 您在健一网的安全验证码为897489,健一网祝您身体健康"; String param="t="+time+"&mobile="+mobile+"&msg="+msg+"&check="+check; String res=HttpSenderUtil.sendPost("http://localhost:8080/ec-dec/page/sms/sendSms/code",param); System.out.println(res); } /** * 执行get方法 * @param url * @param queryString * @return */ public static String doGet(String url, String queryString) { String response = null; HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(url); try { if (StringUtils.isNotBlank(queryString)) method.setQueryString(URIUtil.encodeQuery(queryString)); client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { response = method.getResponseBodyAsString(); } } catch (URIException e) { //logger.error("执行HTTP Get请求时,编码查询字符串“" + queryString + "”发生异常!", e); } catch (IOException e) { //logger.error("执行HTTP Get请求" + url + "时,发生异常!", e); } finally { method.releaseConnection(); } return response; } }
HttpSenderUtil向指定 URL 发送POST方法的请求的更多相关文章
- wemall doraemon中Android app商城系统向指定URL发送GET方法的请求代码
URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...
- 向指定URL发送GET方法获取资源,编码问题。 Rest风格
http编码.今天遇到获取网页上的数据,用HTTP的GET请求访问url获取资源,网上有相应的方法.以前一直不知道什么事rest风格,现在我想就是开一个Controller,然后使人可以调用你的后台代 ...
- 向指定URL发送GET和POST请求
package com.sinosoft.ap.wj.common.util; import java.io.BufferedReader;import java.io.IOException;imp ...
- 向指定URL发送GET、POST方法的请求
/** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param param * 请求参数,请求参数应该是 name1=value1&name ...
- 向指定url发送Get/Post请求
向指定url发送Get/Post请求 1.向指定url发送Get/Post请求 2.HttpUtil 工具类–向指定url发送Get/Post请求 1.向指定url发送Get/Post请求 impor ...
- java利用URL发送get和post请求
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- 向指定URL 发送POST请求的方法
java发送psot请求: package com.tea.web.admin; import java.io.BufferedReader; import java.io.IOException; ...
- 获取域名,url,指定url参数的方法
1.js获取域名的方法 1) var domain = document.domain 2) var domain = window.location.host 2.获取url的方法 1) var u ...
- 向指定url发送请求与获取响应
string url = @"https://www.baidu.com"; //向指定服务器发起请求 HttpWebRequest request = (HttpWebReque ...
随机推荐
- C#后台解析 json 动态解析 通用(Dictionary)
Dictionary<string, object> suggestions = JSONSerializer.Deserialize<Dictionary<string, o ...
- JSON序列——根据JSON生成事务性SQL
JSON序列——根据JSON生成事务性SQL procedure TForm1.Button5Click(Sender: TObject); begin var json: string :='' + ...
- .Net Core URL编码和解码
一.URL说明 .Net Core中http 的常用操作封装在 HttpUtility 中 命名空间 using System.Web; // // 摘要: // Provides methods f ...
- caffe solver 配置详解
caffe solver通过协调网络前向推理和反向梯度传播来进行模型优化,并通过权重参数更新来改善网络损失求解最优算法,而solver学习的任务被划分为:监督优化和参数更新,生成损失并计算梯度.caf ...
- python Image resize 对iOS图片素材进行2X,3X处理
通常在iOS上开发使用的图片素材1x,2x,3x三种 下面利用python Image 库 resize函数,由一个大图,自动生成1x,2x,3x的素材照片: 1. 首先你的python环境要安装有I ...
- 《Unix&Linux大学教程》学习笔记四:标准I/O 与 过滤器
1:标准IO Unix中的标准IO主要包括:标准输入.标准输出(正常输出).标准错误(异常信息) 2:重定向输出 内容 > 文件名 :将内容输出到文件,并且覆盖文件原来内容:文件不存在则新建 内 ...
- 【PMP】变更流程图与说明
点击下载viso原件 以下是变更流程说明: 1.团队成员判定不需要变更 发起人提出变更请求,团队成员对该变更进行分析与评估影响,通过判断后,如果决定不需要变,那么就编写反馈报告提交给相关方确认,相关方 ...
- js 引擎 和 html 渲染引擎
- sqlserver修改主键为自增
使用PowerDesigner创建一张表, 拷贝建表语句发现ID不是自增的, 以下是修改语句: ALTER TABLE USER_JOB_EXE_REC DROP COLUMN id; , ); 注: ...
- 书记查询的App设计
表示学习Andriod没有头绪,看书看着看着都觉得大部分是些item的介绍,看过去之后会完全忘记,还是自己做个小东西进行实际开发学习和掌握起来会比较快. PC端的软件已经做好,当然因为PC屏幕较大,手 ...