apache 의 httpclient 를 자세히 설명 합 니 다.

2342 단어 httpclient

아무 말 도 하지 않 고 코드 를 직접 올 리 면 위 에 주석 이 있 습 니 다.
 
 
	int timeOut = 5000;

		String URL = "http://localhost/xx.do?method=xxx&xxx=XXX"; //        GET   ,  get        URL  

		String charSet = "GBK";

		HttpClient http = new HttpClient(); //     httpClient

		http.getHttpConnectionManager().getParams()
				.setConnectionTimeout(timeOut);//       

		http.getHttpConnectionManager().getParams()
				.setParameter("http.socket.timeout", timeOut);//         

		/*
		 *       get           GETMETHOD*
		 */
		GetMethod method = new GetMethod(URL);//   URL    GETMETHOD
		http.executeMethod(method);//   HTTPCLIENT   URL

		int statusCOde = method.getStatusCode();//   200,404   ,      HttpStatus      

		method.getRequestHeaders();

		method.getResponseBody();

		http.executeMethod(method);

		method.getStatusCode();

		method.releaseConnection();
		/*
		 * method.get                   get           GETMETHOD*
		 */

		/*
		 *       POST           GETMETHOD*
		 */

		PostMethod httppost = new PostMethod(URL);
		Map<String, Object> parameters = new HashMap<String, Object>(); // Post
																		//    ,      

		httppost.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=" + charSet);

		int pairNum = parameters.size();

		NameValuePair[] parametersBody = new NameValuePair[pairNum];

		for (String key : parameters.keySet()) { //     

			String value = (String) parameters.get(key);

			NameValuePair p = new NameValuePair(
					URLEncoder.encode(key, charSet), URLEncoder.encode(value,
							charSet));

			parametersBody[--pairNum] = p;

		}

		httppost.setRequestBody(parametersBody);

		http.executeMethod(httppost);

		httppost.getRequestHeaders();// ......

		httppost.getResponseBodyAsString();// ,,,,...

		//             .        
 
 
 

 

 

 

좋은 웹페이지 즐겨찾기