123

        try {

PostMethod postMethod = null;
postMethod = new PostMethod(url) ;
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
//参数设置,需要注意的就是里边不能传NULL,要传空字符串
NameValuePair[] data = {
new NameValuePair("username","111"),
new NameValuePair("password","222")
};
postMethod.setRequestBody(data);
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();

int response = httpClient.executeMethod(postMethod); // 执行POST方法

String result = postMethod.getResponseBodyAsString() ;
System.out.println(result);
} catch (Exception e) {
System.out.println(1231);

throw new RuntimeException(e.getMessage());
}

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
posted @ 2023-01-30 20:59  1994小剑  阅读(87)  评论(0)    收藏  举报