发送HTTP请求
程序员文章站
2024-01-22 21:36:16
...
public static void main(String[] args) {
String url = "http://ip:port/ssoGetTicket.html";
String username = "test1";
String password = "111222";
String validMin = "30";
HttpClient httpClient = null;
PostMethod postMethod = null;
try {
httpClient = new HttpClient();
httpClient.getParams().setParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, 60 * 1000);
httpClient.getParams().setParameter(
HttpConnectionParams.SO_TIMEOUT, 60 * 1000);
postMethod = new Post Method(url);
Part[] parts = new Part[] {
new StringPart("username", username, "UTF-8"),
new StringPart("password", password, "UTF-8"),
new StringPart("validMin", validMin, "UTF-8") };
MultipartRequestEntity entity = new MultipartRequestEntity(parts,
postMethod.getParams());
postMethod.setRequestEntity(entity);
httpClient.executeMethod(postMethod);
if (postMethod != null && postMethod.getStatusCode() == 200) {
String ticket = postMethod.getResponseBodyAsString();
System.out.println(ticket);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
}
用到的jar包: