首页 > 代码库 > httpclient_1
httpclient_1
用到的jar:commons-codec-1.6.jar、commons-httpclient-3.0.jar、commons-logging-1.1.3.jar
package com.httpclient;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class TT {
public static void main(String[] args) {
TT dp = new TT();
System.out.println(dp.getPack("1039", "1003"));
}
public String getPack(String grade,String subject) {
TreeMap<String,String> map=new TreeMap<String,String>();
map.put("commandid", "guestpacklist");
map.put("grade", grade);
map.put("subject", subject);
NameValuePair[] data = http://www.mamicode.com/new NameValuePair[addSigned(map).keySet().size()];
Iterator it = map.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = http://www.mamicode.com/entry.getValue();
data[i] = new NameValuePair(key.toString(), value.toString());
i++;
}
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://api.xxx.hk/api/android/");
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
post.setRequestBody(data);
try {
client.executeMethod(post);
return post.getResponseBodyAsString();
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();
}
return "nothing response!maybe can`t connect to the host!";
}
public static TreeMap<String, String> addSigned(
TreeMap<String, String> parmas) {
// 前面参数(TreeMap),组合后再拼上key,最后一起加密为signed参数。最后返回之前的参数和signed
String signed = "";
Set<String> key = parmas.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
// P.systemOut(s + " -> ");
// P.systemOut(parmas.get(s));
signed += s + parmas.get(s);
}
signed += "abcde123abcde123abcde123abcde123a";
parmas.put("signed", JT_MD5.encodeString(signed.getBytes()));
return parmas;
}
}
httpclient_1