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();
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值