调用发送短信接口经验总结

短信接口要求基于HTTP协议调用,原来只接触过Post、Get请求方式,没有用过Put请求。

1、请求方式PUT---使用到httpclient-4.2.5.jar和httpcore-4.2.4.jar,参考http://download.csdn.net/download/hellosoftware/8744277

请求头的具体含义可以参考:http://tools.jb51.net/table/http_header

public String sendPUT(String restfulUrl, Map<String, String> params) {
		HttpClient client = new DefaultHttpClient();
		HttpPut requestPut = new HttpPut(restfulUrl);
		//设置请求头,根据接口规范做相应调整
		requestPut.setHeader("Accept","application/json, application/xml, text/html, text/*, image/*, */*");
		final List<BasicNameValuePair> putData = new ArrayList<BasicNameValuePair>();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			putData.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
		}
		if (params != null && !params.isEmpty()) {
			try {
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(putData,
						HTTP.UTF_8);
				requestPut.setEntity(entity);
			} catch (UnsupportedEncodingException ignore) {
				ignore.printStackTrace();
			}
		}
		String result = "";
		try {
			HttpResponse httpResponse = client.execute(requestPut);
			if (httpResponse == null)
				return result;
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
				result = EntityUtils.toString(httpResponse.getEntity());
		} catch (IOException ignore) {
			ignore.printStackTrace();
			if (LOG.isErrorEnabled())
				LOG.error(ignore);
		} finally {
			if (client != null)
				client.getConnectionManager().shutdown();
		}
		return result.toString();
}



2、MD5加密 参考:http://blog.csdn.net/miaoyahong/article/details/50561881

public class MD5Util {  
      
    public static void main(String[] args) {  
        String pwd = getMD5("password");  
        System.out.println(pwd);  
    }  
   
   //生成MD5  
    public static String getMD5(String message) {  
        String md5 = "";  
        try {  
            MessageDigest md = MessageDigest.getInstance("MD5");  // 创建一个md5算法对象  
            byte[] messageByte = message.getBytes("UTF-8");  
            byte[] md5Byte = md.digest(messageByte);              // 获得MD5字节数组,16*8=128位  
            md5 = bytesToHex(md5Byte);                            // 转换为16进制字符串  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return md5;  
    }  
   
     // 二进制转十六进制  
    public static String bytesToHex(byte[] bytes) {  
        StringBuffer hexStr = new StringBuffer();  
        int num;  
        for (int i = 0; i < bytes.length; i++) {  
            num = bytes[i];  
             if(num < 0) {  
                 num += 256;  
            }  
            if(num < 16){  
                hexStr.append("0");  
            }  
            hexStr.append(Integer.toHexString(num));  
        }  
        return hexStr.toString();  
    }  
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值