发送post请求的帮助类

java 接口对接发送post请求的帮助类

package com.card.mSystem.weimob.http;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;

import net.sf.json.JSONObject;

public class HtppUtilsWeimoInte {
	
	

	 
	public static String sendPostMessage(String PATH,JSONObject obj,
			String encode) throws Exception {
		// 表示服务器端的url
		//String PATH = "http://120.24.239.82:8001/ExternalHandler.api?action=GetAllIntroducerCardByIdNo";
		URL url = new URL(PATH);
		HttpURLConnection httpURLConnection = (HttpURLConnection) url
				.openConnection();
		httpURLConnection.setConnectTimeout(3000);
		httpURLConnection.setDoInput(true);// 从服务器获取数据
		httpURLConnection.setDoOutput(true);// 向服务器写入数据

		httpURLConnection.setRequestMethod("POST");
		//str = URLEncoder.encode(obj, "utf-8");byte[] fullByte1 =   
		// 获得上传信息的字节大小及长度
		byte[] mydata = obj.toString().getBytes("utf-8");
		// 设置请求体的类型
		httpURLConnection.setRequestProperty("Content-Type","application/json;charset=utf-8");
		httpURLConnection.setRequestProperty("Content-Lenth", String.valueOf(mydata.length));
		
		// 获得输出流,向服务器输出数据
		OutputStream outputStream = (OutputStream) httpURLConnection.getOutputStream();
		outputStream.write(mydata);

		// 获得服务器响应的结果和状态码
		int responseCode = httpURLConnection.getResponseCode();				
//		if (responseCode == 200) {
			// 获得输入流,从服务器端获得数据
			InputStream inputStream = (InputStream) httpURLConnection.getInputStream();
			return (changeInputStream(inputStream, encode));
//		}
//		return "";
	}
	
	/*
	 * // 把从输入流InputStream按指定编码格式encode变成字符串String
	 */
	public static String changeInputStream(InputStream inputStream,
			String encode) throws Exception {		
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		int len = 0;
		String result = "";
		if (inputStream != null) {			
			while ((len = inputStream.read(data)) != -1) {
				byteArrayOutputStream.write(data, 0, len);
			}
			result = new String(byteArrayOutputStream.toByteArray(), encode);
		}
		return result;
	}
	 /**
     * 将json格式的字符串解析成Map对象 <li>
     * json格式:{"name":"admin","retries":"3fff","testname"
     * :"ddd","testretries":"fffffffff"}
     */
    @SuppressWarnings("unchecked")
	public static HashMap<String, Object> toHashMap(Object object)
    {
        HashMap<String, Object> data = new HashMap<String, Object>();
        // 将json字符串转换成jsonObject
        JSONObject jsonObject = JSONObject.fromObject(object);
        Iterator it = jsonObject.keys();
        // 遍历jsonObject数据,添加到Map对象
        while (it.hasNext())
        {
            String key = String.valueOf(it.next());
            Object value =  jsonObject.get(key);
            data.put(key, value);
        }
        return data;
    }

}

测试发送短信的代码:

package com.card.mSystem.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.net.URLEncoder;

public class TestSMS {
    public static void main(String[] args) {
        try {
           HttpClient client = new HttpClient();
            String url="http://www.139000.com/send/gsend.asp";
            Double money=136.59999999999997;
            String msg="账户变动,变动金额:-0.8,余额:"+Math.round(money * 100) * 0.01d+",变动积分:-0.0,积分:1000.0,时间:2020-04-27 15:42:02";
            System.out.println(msg);
            System.out.println(msg.length());
            url+="?name=nccphomes&pwd=nccphomes9&dst=17674628305&msg="+ URLEncoder.encode(msg, "GB2312");
            GetMethod get = new GetMethod(url);
            HttpMethodParams params = new HttpMethodParams();
            params.setContentCharset("GB2312");
            get.setParams(params);
            client.executeMethod(get);
            String result = new String(get.getResponseBodyAsString().trim()); //返回消息状态
            System.out.println(result);
            get.releaseConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值