我想發送包含許多字符串參數和一個HashMap對象的Post請求。這個怎麼做?如何使用帶有HashMap的HTTPClient作爲一個參數發送POST請求
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
public class PostRequest {
public static void main(String args[]) throws Exception{
String url="http://url";
PostMethod post=new PostMethod(url);
post.setParameter("param1","abc");
post.setParameter("param2","1");
HttpClient httpclient = new HttpClient();
int a = httpclient.executeMethod(post);
System.out.println("I::::::::::::::::" + a);
String postResp = post.getResponseBodyAsString();
System.out.println("response::::" + postResp);
}
}
在上面的代碼中,我也想在請求中發送HashMap對象。
HashMap hm = new HashMap();
hm.put("key","value");
//Set this param in URL.
post.setParameter("paramname",hm);
請幫忙。
你想重新設計你的URL並傳遞給HttpClient? –