HttpURLConnection方式、HttpClient方式发送get、post数据给服务端

public class NewsManageService {
	/**
	 * 保存数据
	 * @param title 标题
	 * @param timelength 时长
	 */
	public static boolean save(String title, String timelength) throws Exception{
		String path = "http://192.168.1.100:8080/videonews/ManageServlet";
		Map<String, String> params = new HashMap<String, String>();
		params.put("title", title);
		params.put("timelength", timelength);
		return sendHttpClientPOSTRequest(path, params, "UTF-8");
	}
	/**
	 * 采用HttpClient发送POST请求
	 * @param path 请求路径
	 * @param params 请求参数
	 * @throws Exception
	 */
	private static boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
		List<NameValuePair> param = new ArrayList<NameValuePair>();
		if(params!=null && !params.isEmpty()){
			for(Map.Entry<String, String> entry : params.entrySet()){
				param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
		}
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(param, 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;
	}
	/**
	 * 发送POST请求
	 * @param path 请求路径
	 * @param params 请求参数
	 * @throws Exception
	 */
	private static boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
		StringBuilder sb = new StringBuilder();
		// title=kkkkk&timelength=50
		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[] entity = sb.toString().getBytes();
		HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);//允许对外输出数据
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
		OutputStream outStream = conn.getOutputStream();
		outStream.write(entity);
		if(conn.getResponseCode() == 200){
			return true;
		}
		return false;
	}
	
	
	/**
	 * 发送GET请求
	 * @param path 请求路径
	 * @param params 请求参数
	 * @throws Exception
	 */
	private static boolean sendGETRequest(String path, Map<String, String> params, String encoding) throws Exception{
		// http://192.168.1.100:8080/videonews/ManageServlet?title=xxx&timelength=30
		StringBuilder sb = new StringBuilder(path);
		sb.append('?');
		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);
		HttpURLConnection conn = (HttpURLConnection) new URL(sb.toString()).openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		if(conn.getResponseCode() == 200){
			return true;
		}
		return false;
	}
	
	public static void save(String title, String timelength, File file) throws Exception{
		String path = "http://192.168.1.100:8080/videonews/ManageServlet";
		Map<String, String> params = new HashMap<String, String>();
		params.put("title", title);
		params.put("timelength", timelength);
		FormFile formFile = new FormFile(file, "videofile", "audio/mpeg");
		SocketHttpRequester.post(path, params, formFile);
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值