欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

httpClient应用

程序员文章站 2022-05-12 09:15:33
...
package com.http.client;

import org.apache.commons.digester.Digester;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TopUpClient {
	private String url = "http://10.0.3.65:8081/web/api/topup/";

	private static String key = MD5
			.encrypt("civony_{00100-15222-222-55220}");

	private HttpClient client;
	private PostMethod post;
	private String orderid;
	private String money;
	private String aumont;
	private String payaccount;
	private String paytype;
	private String sign;

	@Before
	public void init() {
		client = new HttpClient();
		post = new PostMethod(url);

		orderid = "111222323334";
		money = "200.32";
		aumont = "1000";
		payaccount = "5dsdd";
		paytype = "1";
		sign = MD5.encrypt(orderid + money + aumont + payaccount + paytype
				+ key);
	}

	@Test
	public void rightRequest() {
		try {
			NameValuePair[] data = { new NameValuePair("money", money),
					new NameValuePair("orderid", orderid),
					new NameValuePair("aumont", aumont),
					new NameValuePair("payaccount", payaccount),
					new NameValuePair("paytype", paytype),
					new NameValuePair("sign", sign) };
			post.setRequestBody(data);
			int statusCode = client.executeMethod(post);
			System.out.println(statusCode);
			
			String result = post.getResponseBodyAsString();
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@After
	public void release(){
		if(post!=null){
			post.releaseConnection();
		}
	}
}
相关标签: junit Apache Web