HttpURLConnection 的get跟post请求

本文详细介绍了使用HttpURLConnection进行GET和POST请求的方法,包括请求路径、参数处理及响应码判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HttpURLConnection 的get和post请求

	/**
	 * 发出一个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;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值