發送HTTP請求

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包:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章