java 短信平台接口_短信平台接口调用方法(java语言)

//说明:此处需引用httpclient、httpcore、commons-logging三个jar包

importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;import java.util.*;importjava.security.MessageDigest;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.message.BasicNameValuePair;import org.apache.http.*;importjavax.crypto.SecretKey;importjavax.crypto.spec.DESKeySpec;importjavax.crypto.spec.IvParameterSpec;importjavax.crypto.SecretKeyFactory;importjavax.crypto.Cipher;public static voidmain(String[] args) {

String url="http://xxx.com/api/MsgSend.asmx/SendMes";

List nvps = new ArrayList();

nvps.add(new BasicNameValuePair("userCode", "用户名"));

nvps.add(new BasicNameValuePair("userPass", "密码"));

nvps.add(new BasicNameValuePair("DesNo", "手机号"));

nvps.add(new BasicNameValuePair("Msg", "短信内容【签名】"));

nvps.add(new BasicNameValuePair("Channel", "通道号"));

String post=httpPost(url,nvps); //post请求

String getparam="userCode=用户名&userPass=密码&DesNo=手机号&Msg=短信内容【签名】&Channel=通道号";

String result=httpGet(url,getparam); //get请求

}public static String httpPost(String url,Listparams) {

String result= "";try{

HttpClient httpclient= newDefaultHttpClient();

HttpPost httpPost= newHttpPost(url);

httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

HttpResponse response=httpclient.execute(httpPost);

HttpEntity entity=response.getEntity();if (entity != null) {

InputStream instreams=entity.getContent();

result=convertStreamToString(instreams);

System.out.println(result);

}

}catch(Exception e) {

}returnresult;

}public staticString httpGet(String url,String params){

String result="";try{

HttpClient client=newDefaultHttpClient();if(params!=""){

url=url+"?"+params;

}

HttpGet httpget=newHttpGet(url);

HttpResponse response=client.execute(httpget);

HttpEntity entity=response.getEntity();if (entity != null) {

InputStream instreams=entity.getContent();

result=convertStreamToString(instreams);

System.out.println(result);

}

}catch(Exception e){}returnresult;

}public staticString convertStreamToString(InputStream is) {

BufferedReader reader= new BufferedReader(newInputStreamReader(is));

StringBuilder sb= newStringBuilder();

String line= null;try{while ((line = reader.readLine()) != null) {

sb.append(line+ "\n");

}

}catch(IOException e) {

e.printStackTrace();

}finally{try{

is.close();

}catch(IOException e) {

e.printStackTrace();

}

}returnsb.toString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值