package tea.ui.neea.chaxun;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
public class SMSMessage {
private String projectId;//
private String smsType;
private String traceNo;
private String applicationId;
private String extCode;
private String receiverMobileNo;
private String smsContent;
private String status;
private String message;
/**
* 获取短信接口需要参数类
* @param mobile 手机号
* @param content 短信内容
* @return
*/
private SMSMessage getSMSMessage(String mobile,String content){
SMSMessage smsMessage = new SMSMessage();
smsMessage.setProjectId("XXX");
smsMessage.setSmsType("XXX");
UUID uuid = UUID.randomUUID();
smsMessage.setTraceNo(uuid.toString());
smsMessage.setApplicationId("XXX");
smsMessage.setExtCode("XX");
smsMessage.setReceiverMobileNo(mobile);
//base64加密短信内容
smsMessage.setSmsContent(Base64.encodeBase64String(content.getBytes()));
return smsMessage;
}
private List<NameValuePair> getParmList(SMSMessage smsMessage) {
List<NameValuePair> parmList = new ArrayList<NameValuePair>();
parmList.add(new BasicNameValuePair("projectId", smsMessage.getProjectId()));
parmList.add(new BasicNameValuePair("smsType", smsMessage.getSmsType()));
parmList.add(new BasicNameValuePair("traceNo", smsMessage.getTraceNo()));
parmList.add(new BasicNameValuePair("applicationId", smsMessage.getApplicationId()));
parmList.add(new BasicNameValuePair("extCode", smsMessage.getExtCode()));
parmList.add(new BasicNameValuePair("receiverMobileNo", smsMessage.getReceiverMobileNo()));
parmList.add(new BasicNameValuePair("smsContent", smsMessage.getSmsContent()));
return parmList;
}
private String getParms(SMSMessage smsMessage){
JSONObject obj = new JSONObject();
try {
obj.put("projectId", smsMessage.getProjectId());
obj.put("smsType", smsMessage.getSmsType());
obj.put("traceNo", smsMessage.getTraceNo());
obj.put("applicationId", smsMessage.getApplicationId());
obj.put("extCode", smsMessage.getExtCode());
obj.put("receiverMobileNo", smsMessage.getReceiverMobileNo());
obj.put("smsContent", smsMessage.getSmsContent());
} catch (JSONException e) {
e.printStackTrace();
}
return obj.toString();
}
private void sendMessage(String mobile,String content) {
String uri = "http://ip:99/soa/sms";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(uri);//创建HttpPost对象
RequestConfig config = RequestConfig.custom()
.setConnectionRequestTimeout(10000).setConnectTimeout(10000)
.setSocketTimeout(10000).build();//设置请求和传输超时时间
CloseableHttpResponse response = null;
try {
SMSMessage smsMessage = getSMSMessage(mobile,content);
/*List<NameValuePair> parmList = getParmList(smsMessage);
post.setEntity(new UrlEncodedFormEntity(parmList, "UTF-8"));
普通表单,纯参数了,键值对,服务端直接获取request.getParameter
*/
//http post的json数据格式
String parms = getParms(smsMessage);
post.setEntity(new StringEntity(parms));
/*
Enumeration en = request.getParameterNames();
while (en.hasMoreElements()) {
String paramName = (String) en.nextElement();
String paramValue = request.getParameter(paramName);
}
*/
post.setConfig(config);
post.addHeader("Content-Type", "application/json");
post.addHeader("UserName", "XXX");
post.addHeader("Password", "XXX");
response = httpClient.execute(post);//执行请求
HttpEntity entity = response.getEntity();
if(response.getStatusLine().getStatusCode() == 200){
if (entity != null) {//如果没有错误返回码
String json_str = EntityUtils.toString(entity);
System.out.println("content:" + content);
String info = "";
if(json_str!=null){
JSONObject obj = new JSONObject(json_str);
String status = (String)obj.get("status");
String message = (String)obj.get("message");
info = "status:"+status+",message:"+message;
System.out.println(info);
}
}
}
EntityUtils.consume(entity);//关闭HttpEntity的流
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SMSMessage smsMessage = new SMSMessage();
smsMessage.setReceiverMobileNo("13012345678");
smsMessage.setSmsContent("测试短信");
smsMessage.sendMessage("13012345678", "测试短信");
}
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
public String getSmsType() {
return smsType;
}
public void setSmsType(String smsType) {
this.smsType = smsType;
}
public String getTraceNo() {
return traceNo;
}
public void setTraceNo(String traceNo) {
this.traceNo = traceNo;
}
public String getApplicationId() {
return applicationId;
}
public void setApplicationId(String applicationId) {
this.applicationId = applicationId;
}
public String getExtCode() {
return extCode;
}
public void setExtCode(String extCode) {
this.extCode = extCode;
}
public String getReceiverMobileNo() {
return receiverMobileNo;
}
public void setReceiverMobileNo(String receiverMobileNo) {
this.receiverMobileNo = receiverMobileNo;
}
public String getSmsContent() {
return smsContent;
}
public void setSmsContent(String smsContent) {
this.smsContent = smsContent;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
相关推荐
总结起来,使用Apache HTTPClient库以POST方式发送JSON数据涉及的主要步骤包括:配置HttpClient实例、创建HttpPost对象、构建JSON实体、设置请求头和执行请求。通过这种方式,你可以方便地与Web服务进行交互,传递...
HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(params, Charset.forName("UTF-8")); httpPost.setHeader("Content-type", "application/json"); httpPost.setEntity...
### Java HttpClient 发送GET请求和带有表单参数的POST请求详解 #### 一、概述 在Java编程中,处理HTTP请求是一项常见的需求,特别是在与Web服务进行交互时。Apache HttpClient库提供了一种强大的方法来执行HTTP...
本篇文章将深入探讨如何使用HTTPClient发送POST请求,以及相关的项目结构和实现细节。 首先,"post-demo"项目定义了我们需要调用的接口。通常,这会是一个Web服务,可能是一个RESTful API,提供特定的功能或数据。...
对于POST请求,我们需要创建一个HttpPost对象,并设置请求URL。如果需要发送JSON数据,我们可以使用StringEntity: ```java HttpPost httpPost = new HttpPost("http://example.com"); httpPost.setHeader("Accept...
在使用HttpClient发送POST请求时,我们需要关心两个方面的问题:一是如何忽略证书验证过程,二是如何发送POST请求。对于前者,我们可以使用X509TrustManager来忽略证书验证过程。对于后者,我们可以使用HttpPost对象...
3. **HttpClient POST请求**: POST请求常用于向服务器提交数据。下面是一个POST请求的例子,包括设置请求头和请求体: ```java import org.apache.http.HttpEntity; import org.apache....
本文将深入探讨如何使用HttpClient进行GET和POST请求,并提供相关的代码示例。 首先,GET请求是最常见的HTTP请求类型,通常用于获取资源。在HttpClient中,发送GET请求可以通过`HttpGet`类实现。以下是一个简单的...
1. **HttpClient的基本使用**:介绍HttpClient类的基本概念,包括如何创建实例,设置请求头,以及发送GET和POST请求。 2. **封装的设计**:讨论如何设计一个网络请求的通用接口或类,比如定义方法如`SendGetAsync`...
对于POST请求,你需要创建一个HttpPost对象,并设置请求体: ```java HttpPost httpPost = new HttpPost("http://example.com"); List<NameValuePair> params = new ArrayList(); params.add(new ...
在这个场景中,我们关注的是如何使用HttpClient来发送POST请求并传输JSON数据。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,广泛用于API接口的数据传递。 首先,我们需要引入Apache HttpClient...
利用httpclient-4.3.1.jar、httpcore-4.3.jar包,很简单的用java实现httpget和httppost请求。
httpclient get/post请求工具类(map参数封装),方便调用
httpclient的用法,发送get请求和post请求,设置header
总的来说,HttpClient是Java开发者进行HTTP通信的强大工具,而HttpPost和HttpGet是其主要的请求方式。确保正确引入并配置所需的jar包,可以让我们轻松地与Web服务进行数据交换。在实际项目中,根据需求,可能还需要...
以下是一个使用HttpClient发送POST请求的Java代码示例: ```java import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import...
它创建一个 `HttpPost` 实例,并设置请求的 URL。如果参数 Map 不为空,它会将参数转换为 `NameValuePair` 列表,然后用 `UrlEncodedFormEntity` 封装,设置为请求实体。这适用于发送 URL 编码的表单数据。 - 如果...
接下来,我们将分别讲解HttpClient如何实现GET和POST请求: 1. GET请求: 使用HttpGet类创建GET请求,指定目标URL。例如: ```java HttpGet httpGet = new HttpGet("http://example.com"); CloseableHttpClient...
mfc http post 和get 请求