HttpURLConnection의 get 및 post 요청

3455 단어
/**
	 *  GET 
	 * @param path  , 
	 * @param params  
	 * @return
	 * @throws Exception
	 */
	private static boolean sendGetRequest(String path, Map<String, String> params, String encoding) throws Exception{
		// http://192.168.1.10:8080/videoweb/video/manage.do?method=save&name=xxx&timelength=90
		StringBuilder pathBuilder = new StringBuilder(path);
		pathBuilder.append('?');
		for(Map.Entry<String, String> entry : params.entrySet()){
			pathBuilder.append(entry.getKey()).append('=')
			.append(URLEncoder.encode(entry.getValue(), encoding)).append('&');
		}
		pathBuilder.deleteCharAt(pathBuilder.length() - 1);
		URL url = new URL(pathBuilder.toString());
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(5*1000);
		if(conn.getResponseCode()==200){
			return true;
		}
		return false;
	}
	
	/**
	 *  POST 
	 * @param path  , 
	 * @param params  
	 * @return
	 * @throws Exception
	 */
	private static boolean sendPostRequest(String path, Map<String, String> params, String encoding) throws Exception{
		// method=save&name=liming&timelength=78
		StringBuilder sb = new StringBuilder("");
		if(params!=null && !params.isEmpty()){
			for(Map.Entry<String, String> entry : params.entrySet()){
				sb.append(entry.getKey()).append('=')
				.append(URLEncoder.encode(entry.getValue(), encoding)).append('&');
			}
			sb.deleteCharAt(sb.length() - 1);
		}
		byte[] data = sb.toString().getBytes();// 
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(5*1000);
		conn.setDoOutput(true);// post , 
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length", String.valueOf(data.length));
		OutputStream outStream = conn.getOutputStream();
		outStream.write(data);
		outStream.flush();
		outStream.close();
		if(conn.getResponseCode()==200){
			return true;
		}
		return false;
	}
	// HTTPS SSL  COOKIE
	private static boolean sendPostRequestHttpClient(String path, Map<String, String> params, String encoding) throws Exception{
		List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
		if(params!=null && !params.isEmpty()){
			for(Map.Entry<String, String> entry : params.entrySet()){
				BasicNameValuePair param = new BasicNameValuePair(entry.getKey(), entry.getValue());
				paramPairs.add(param);
			}
		}
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramPairs, encoding);
		HttpPost post = new HttpPost(path);
		post.setEntity(entity);
		DefaultHttpClient client = new DefaultHttpClient();// 
		HttpResponse response = client.execute(post);		
		if(response.getStatusLine().getStatusCode()==200){
			return true;
		}
		return false;
	}

좋은 웹페이지 즐겨찾기