接口测试类(http,post请求)参数 json格式
博客专区 > gulf 的博客 > 博客详情
接口测试类(http,post请求)参数 json格式
gulf 发表于10个月前
接口测试类(http,post请求)参数 json格式
  • 发表于 10个月前
  • 阅读 113
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

package RMI;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import net.sf.json.JSONObject;

public class TestHttp {

    public static final String ADD_URL = "http://127.0.0.1:8080/IVRClient/IVRInterface/clearLogin.do";
    
    
    public static void appadd() {

        try {
            //创建连接
            URL url = new URL(ADD_URL);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type",
                    "application/json");

            connection.connect();

            //POST请求
            DataOutputStream out = new DataOutputStream(
                    connection.getOutputStream());
            JSONObject obj = new JSONObject();
            obj.accumulate("cardNo", "1609231000030356");
            obj.accumulate("cardKind", "1");
            
            obj.accumulate("accountNo", "1234");
            obj.accumulate("userNo", "20160921161658194471");
            obj.accumulate("password", "123456");
            obj.accumulate("year", "2016");
            obj.accumulate("month", "06");
            //IVR测试数据
            obj.accumulate("type", "4");
            obj.accumulate("code", "20170223102947926194");
            obj.accumulate("telephone", "135888888888");
            obj.accumulate("bankNo", "32432432517022310294792");
            
            out.writeBytes(obj.toString());
            System.out.println(obj.toString());
            out.flush();
            out.close();

            //读取响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String lines;
            StringBuffer sb = new StringBuffer("");
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes(), "utf-8");
                sb.append(lines);
            }
            System.out.println(sb);
            reader.close();
            // 断开连接
            connection.disconnect();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        appadd();
    }

}

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 5
博文 207
码字总数 117291
×
gulf
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: