post请求方式写法--json和表单提交

 下面第一个 是json类型的

public static String send(String url,String data) throws DJException{
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost post = null;
		CloseableHttpResponse response=null;
		try {
			post = new HttpPost(url);  // 请求方法Post
			StringEntity entity = new StringEntity(data,"utf-8");
			entity.setContentEncoding("UTF-8");    // 字符编码
			entity.setContentType("application/json");   // json类型
			post.setEntity(entity);
			response =httpClient.execute(post);
			int statusCode=	response.getStatusLine().getStatusCode(); // 返回的状态码
			if(statusCode == 200) {
				HttpEntity he = response.getEntity();
				return EntityUtils.toString(he, "UTF-8");
			}else{
				throw new DJException("服务器连接异常statusCode:"+statusCode);
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new DJException("请求失败:"+e.getMessage());
		}finally {
			try {
				response.close();
				httpClient.close();
			} catch (IOException e) {
			}	
		}
		
	}

第二个是传入的参数是表单类型的  两种方式,区别不大

类型为from-data数据格式的请求方式
 /**
     * 类型为from-data数据格式的请求方式
     * @param url
     * @return
     * @throws Exception
     */
	public static String send(String url,Map<String,String> map) throws Exception{
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost post = null;
		CloseableHttpResponse response=null;
		try {
			post = new HttpPost(url);
            // params添加表单数据 作为参数
			List<NameValuePair> params = new ArrayList();
			for (String parameter: map.keySet()) {
				params.add(new BasicNameValuePair(parameter, map.get(parameter)));
			}
            UrlEncodedFormEntity  entity = new UrlEncodedFormEntity(params,"UTF-8");
			entity.setContentEncoding("UTF-8");
			entity.setContentType("application/x-www-form-urlencoded");
			post.setEntity(entity);
			response =httpClient.execute(post);
			int statusCode=	response.getStatusLine().getStatusCode();
			if(statusCode == 200) {
				HttpEntity he = response.getEntity();
				return EntityUtils.toString(he, "UTF-8");
			}else{
				throw new DJException("服务器连接异常statusCode:"+statusCode);
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new DJException("请求失败:"+e.getMessage());
		}finally {
			try {
				response.close();
				httpClient.close();
			} catch (IOException e) {
			}
		}

	}



	/*
	 * **HttpClient Post 以表单提交方式请求 带参数**
	 */
	public static String send2(String url,Map<String,Object> map) throws DJException, IOException{
		//1、创建HttpClient
		org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
		//2、创建get或post请求方法
		PostMethod method = new PostMethod(url);
		//3、设置编码
		httpClient.getParams().setContentCharset("UTF-8");
		//4、设置请求消息头,为表单方式提交
		method.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Object> entry = it.next();
			//5、设置参数
			//method.setRequestHeader(key,value);
			method.setParameter(entry.getKey(), entry.getValue().toString());
		}


//		6、执行提交
		httpClient.executeMethod(method);
		int statusCode= method.getStatusLine().getStatusCode();
		if(statusCode == 200) {
			String responseBodyAsString = method.getResponseBodyAsString();
			return responseBodyAsString;
		}else{
			throw new DJException("服务器连接异常statusCode:"+statusCode);
		}
	}

其他资料:https://www.cnblogs.com/hanyj123/p/9641626.html

https://www.cnblogs.com/shengwei/p/5527394.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值