package testaxis;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
//封装的post提交方法
//参数url为需要去请求的webservice接口
//map里面放的是请求的webservice接口所需的参数,
//返回的Sring 是一个xml配置文件组成的
public class getPostResponse {
private String getPostResponse(String url, Map
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List
Iterator
while (it.hasNext()) {
String key = it.next();
NameValuePair data = new BasicNameValuePair(key, params.get(key));
parameters.add(data);
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
httppost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httppost);
HttpEntity myentity = httpResponse.getEntity();
String en=EntityUtils.toString(myentity);
return en;
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
public static void main(String[] args) {
getPostResponse g= new getPostResponse();
// 以下是测试通过传入qq号去获取qq的在线状态返回一个xml配置文件
//把qq号放入Map集合里
Map params=new HashMap();
params.put("qqCode", "8698053");
System.out.println(g.getPostResponse("http://webservice.webxml.com.cn/webservices/qqOnline WebService.asmx/qqCheckOnline", params));
//获得中国省份、直辖市、地区和与之对应的ID,这个接口不需要参数,返回一个xml配置文件
Map params2=new HashMap(); System.out.println(g.getPostResponse("http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince", params));
}
}