java向某地址发送http的POST请求的示例

本文将展示如何使用Java发送HTTP POST请求到指定URL。通过Java的HttpURLConnection或HttpClient库,可以轻松完成此操作,包括设置请求头和发送JSON或表单数据。详细步骤和示例代码将帮助开发者理解并应用到实际项目中。
	/**
	 * @param url 请求的http地址
	 * @param sendJs 要发送的数据
	 * @return 响应数据
	 * @throws ParseException
	 * @throws IOException
	 */
	public static String sendHttpPost(String url, JSONObject sendJs) throws ParseException, IOException {
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		StringEntity entityParams = new StringEntity(sendJs.toJSONString(), "utf-8");
		httpPost.setEntity(entityParams);
		HttpResponse httpResponse;
		httpResponse = httpClient.execute(httpPost);
		HttpEntity entity = httpResponse.getEntity();
		String resData = EntityUtils.toString(entity);
		return resData;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值