用java发送post和get请求的两种方式
1.URL
2.httpclient
第一种方式:
第二种方式:
commons-httpclient-3.1.jar;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class Test {
public static void main(String[] args) {
String info = null;
try {
HttpClient httpclient = new HttpClient();
PostMethod post = new PostMethod("http://192.168.11.11/sssssssss/ssAction!updateYunMasCallBack.action");//
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "gbk");
post.addParameter("SpCode", "sssssss");
post.addParameter("LoginName", "ssss");
post.addParameter("Password", "ssssss");
post.addParameter("MessageContent", "你有一项编号为23你56的事务需要处理。");
post.addParameter("UserNumber", "15680805571");
post.addParameter("SerialNumber", "12345678901234567890");
post.addParameter("ScheduleTime", "");
post.addParameter("ExtendAccessNum", "");
post.addParameter("f", "1");
httpclient.executeMethod(post);
info = new String(post.getResponseBody(), "gbk");
System.out.println(info);
} catch (Exception e) {
e.printStackTrace();
}
}
}