java的HttpClient4.5对接luosimao.com短信平台的短信接口

最近在帮公司使用Java做短信验证的对接业务,网上找到一个使用比较广的短信推送平台螺丝帽(Luosimao.com)该平台提供Java对接开发文档是使用Jersey框架来实现的。按照平台上提供的文档,很容易就对接上了。但是我想到现在Java发送http请求流行使用HttpClient这种更轻量级的专用框架。于是我就试着使用httpClient来实现对接。发现也很容易实现。为了使自己不忘记,在这里记录一下。

maven依赖如下,也可以直接去maven repository下载jar包导入:
 <dependency>
     <groupId>org.apache.httpcomponents</groupId>
     <artifactId>httpclient</artifactId>
     <version>4.5.1</version>
 </dependency>

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
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;

public class Message {
	public static void main(String[] args) throws UnsupportedEncodingException {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//填写发送短信的号码
		params.add(new BasicNameValuePair("mobile", "155xxxxxxxx"));
		//测试默认签名。在平台注册之后可以添加自定义签名,可发送自定义内容
		params.add(new BasicNameValuePair("message", "验证码:286221【铁壳测试】"));
		HttpEntity reqEntity = new UrlEncodedFormEntity(params, "UTF-8");
		sendPost(reqEntity);
	}

	public static void sendPost(HttpEntity reqEntity) {
		//luosimao短信平台短信发送接口URL
		HttpPost post = new HttpPost("http://sms-api.luosimao.com/v1/send.json");
		
		//“d609b769db914a4d959bae3414ed1f7X” --APIkey,在luosimao.com注册登陆以后可以得到
		post.setHeader("Authorization",	"Basic " + Base64.encodeBase64String("api:key-d609b769db914a4d959bae3414ed1f7X".getBytes()));
		post.setEntity(reqEntity);
		try {
			CloseableHttpClient httpClient = HttpClients.createDefault();
			HttpResponse response = httpClient.execute(post);
			HttpEntity respEntity = response.getEntity();
			//status code,如200
			int statusCode= response.getStatusLine().getStatusCode();
			//result,如{"error":0,"msg":"ok"}
			String respString = EntityUtils.toString(respEntity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			post.releaseConnection();
		}
	}
}


在Apache HttpClient 4.5.14版本中,处理HTTP请求通常会涉及到`HttpRequestEntity`的创建,特别是当需要发送POST、PUT等带实体的数据请求时。这里是一个基本的例子,展示如何使用`StringRequestEntity`: ```java import org.apache.http.HttpEntity; 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.util.EntityUtils; public class HttpClientExample { private static final CloseableHttpClient httpClient = HttpClients.createDefault(); public void sendPost(String url, String requestBody) throws Exception { // 创建HttpPost请求 HttpPost httpPost = new HttpPost(url); // 将字符串转换为StringRequestEntity StringEntity stringEntity = new StringEntity(requestBody, "UTF-8"); stringEntity.setContentType("application/x-www-form-urlencoded"); // 设置Content-Type // 设置请求体 httpPost.setEntity(stringEntity); try (CloseableHttpResponse response = httpClient.execute(httpPost)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= 200 && statusCode < 300) { System.out.println("Response status: " + statusCode); HttpEntity entity = response.getEntity(); if (entity != null) { String responseBody = EntityUtils.toString(entity, "UTF-8"); System.out.println("Response body: " + responseBody); } } else { throw new RuntimeException("Failed to execute request, status code: " + statusCode); } } } // 关闭连接池 public void shutDown() throws IOException { httpClient.close(); } } ``` 在这个例子中,你需要替换`url`和`requestBody`为你实际的URL和待发送的数据。当你完成操作后别忘了调用`shutDown()`关闭连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值