post请求表携带单参数

post请求表携带单参数

在这里插入代码片
public class TestLogin {

    private static Logger logger = LoggerFactory.getLogger(TestLogin.class);    //日志记录

    private String url;
    private ResourceBundle rb;
    private CookieStore store;

@BeforeTest
    public void beforeTest() throws Exception {
        rb = ResourceBundle.getBundle("application", Locale.CHINA);
        url = rb.getString("test.url");
        HttpGetClient.testStart(url);
    }

 @Test
    public void testLoginMutli() throws IOException {
        String uri = rb.getString("test.login");
        String testUrl = this.url + uri;
        logger.info(testUrl);
        Map<String,String> myMap = new HashMap<>();
        myMap.put("username","admin");
        myMap.put("password","admin123");
        myMap.put("verifycode","0000");
        HttpPost mypost = new HttpPost(testUrl);
        List<BasicNameValuePair> pairName = new ArrayList<BasicNameValuePair>();
        for(String key : myMap.keySet()){
            pairName.add(new BasicNameValuePair(key,myMap.get(key)));
        }

        UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(pairName,"utf-8");
        mypost.setEntity(uefe);
        CloseableHttpClient client = HttpClients.createDefault();
        HttpResponse response = client.execute(mypost);
        logger.info(String.valueOf(response.getStatusLine().getStatusCode()));
    }
}

post请求携带json参

    @Test
    public void testLoginJson() throws IOException {
        String uri = rb.getString("test.login");
        String testUrl = this.url + uri;
        logger.info(testUrl);
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost mypost = new HttpPost(testUrl);
        JSONObject param = new JSONObject();
        param.put("username","admin");
        param.put("password","admin123");
        param.put("verifycode","0000");
        StringEntity myentity = new StringEntity(param.toString(),"utf-8");
        myentity.setContentType("application/json");
        myentity.setContentEncoding("UTF-8");
        mypost.setEntity(myentity);
        HttpResponse response = client.execute(mypost);
        String result ;

        result = EntityUtils.toString(response.getEntity(),"utf-8");
        logger.info(result);
        System.out.println(result);
//        JSONObject jsonResult = new JSONObject(result);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值