httpclient测试请求方法

貌似很多人不知道服务器端代码怎么写,在此mark一下:
客户端:
ContentType contentType = ContentType.create( 
HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8); 
MultipartEntity entity = new MultipartEntity();
ContentBody fileBody = new FileBody(file);
entity.addPart("file", fileBody);
ContentBody signBody = new StringBody("dolemifasolaxi",contentType);
entity.addPart("SIGN", signBody);
ContentBody imeiBody = new StringBody("xilasofamiledo",contentType);
entity.addPart("IMEI", imeiBody);
服务器代码:
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
String imei = multipartRequest.getParameter("IMEI");
System.out.println("imei:" + imei);
String sign = multipartRequest.getParameter("SIGN");
System.out.println("sign:" + sign);

 HttpClient get调用方式

package com.jshx.http.utils;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;


public class Test {
	private void test(){
		HttpClient client = new HttpClient();
		String queryString = "{\"className\":\"QueryLaw\",\"userId\":\"5\",\"search\":\"6auY5by6\",\"pageSize\":\"10\",\"pageNum\":\"1\"}";
		String url = "http://localhost:8080/jsp/http/json_command.action?strTemp=";
		GetMethod get=new GetMethod(url+queryString);
		System.out.println("para="+queryString);
		String result = "";
		try {
			client.executeMethod(get);
			result = new String(get.getResponseBody(),"UTF-8");
			System.out.println("restConn-----------------------------------"+result);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Test test = new Test();
		test.test();
	}
}

HttpClient post调用方式

package com.jshx.http.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class postTest {
	private void test(){		
		HttpClient client = new HttpClient();
		String queryString = "{\"userId\":\"20130705144110|18963603541\",\"phoneName\":\"华为190\",\"phoneVersion\":\"v.1.1\",\"className\":\"SaveQuick\",\"type\":\"1\",\"queryMonth\":\"2013-02\",\"startTime\":\"2013-02-03 11:16\",\"endTime\":\"2013-02-03 14:16\"}";
		PostMethod get = new PostMethod("http://localhost:8080/servlet/JsonServlet");
		
		NameValuePair[] data = new NameValuePair[1];
		NameValuePair nameValuePair1 = new NameValuePair("jsonStr", queryString);
		//NameValuePair nameValuePair2 = new NameValuePair("docId", "42");
		data[0]=nameValuePair1;
		//data[1]=nameValuePair2;
		get.setRequestBody(data);
		System.out.println("para="+queryString);
		String result = "";
		
		try{
			client.executeMethod(get);
			//byte[] responseBody = get.getResponseBody();
			StringBuffer sb = new StringBuffer();
			sb.append(get.getResponseBody());
			result = new String(get.getResponseBody(), "utf-8");
			System.out.println("result="+result);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public static void main(String[] args) {
		postTest pt = new postTest();
		pt.test();
	}
}

相关推荐