JAVA实现httpClient实例Java认证考试
文章作者 100test 发表时间 2010:01:02 16:12:22
来源 100Test.Com百考试题网
package net.xfok.http.
import java.util.HashMap. import java.util.Iterator. import java.util.Map.
import org.apache.commons.httpclient.Header. import org.apache.commons.httpclient.HttpClient. import org.apache.commons.httpclient.HttpStatus. import org.apache.commons.httpclient.NameValuePair. import org.apache.commons.httpclient.methods.PostMethod.
/**
*/
public class MyClient {
/**
*的到Http请求结果
* @param url请求地址
* @param parms请求参数
* @return
*/
public byte[] getBody(String url,Map parms){
byte[] body=null.
//构造HttpClient的实例
HttpClient httpClient = new HttpClient().
//创建GET方法的实例
PostMethod postMethod = new PostMethod(url).
//填入各个表单域的值
NameValuePair[] data = new NameValuePair[parms.keySet().size()]. Iterator it = parms.entrySet().iterator().
int i=0.
while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(). Object key = entry.getKey(). Object value = entry.getValue(). data[i]=new NameValuePair(key.toString(),value.toString()).
i .
}
//将表单的值放入postMethod中
postMethod.setRequestBody(data).