package cn.com.zhang.httpClientMy;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
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 HttpTest {
public static String post() {
HttpClient httpClient = new HttpClient();
String url = "http://haoma.imobile.com.cn/index.php";
NameValuePair nameValuePair = new NameValuePair("mob", "1364321");
PostMethod postMethod = new PostMethod(url);
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try {
postMethod.setRequestBody(new NameValuePair[] { nameValuePair });
int status = httpClient.executeMethod(postMethod);
if (status != 200) {
return null;
}
String content = "";
content = postMethod.getResponseBodyAsString();
if (content != null) {
System.out.println("========服务器返回内容======");
System.out.println(content);
}
return content;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
postMethod.releaseConnection();
}
}
public static void main(String[] args) {
post();
}
}
HttpClient Post提交实例
最新推荐文章于 2021-09-10 10:42:57 发布