调取第三方接口的post及get方法

该代码段展示了如何使用Java发送POST和GET请求到server端并处理返回的响应。主要涉及URL连接、设置请求头、处理编码问题以及读取响应内容。在POST请求中,将参数写入输出流;在GET请求中,通过URL直接传递参数。
摘要由CSDN通过智能技术生成
/**
	 * 向server端发送请求,并获取返回参数
	 * 
	 * @param actionUrl
	 * @param method
	 * @param param
	 * @return
	 */
	public static String sendPostRequest(String serverUrl, String param) {
		logger.debug("+++++++++++++++sendPostRequest方法开始!+++++++++++++++++++");
		StringBuffer sb = new StringBuffer();
		String output = "";
		try {
			URL url = new URL(serverUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Charset", "utf-8");
			conn.setRequestProperty("Content-Type", "application/json");
			
			//解决乱码代码
			conn.setRequestProperty("Accept-Charset", "utf-8");
			conn.setRequestProperty("contentType", "utf-8");
			
			if (param != null) {
				OutputStream os = conn.getOutputStream();
				os.write(param.getBytes("utf-8"));
				os.flush();
			}
			BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			while ((output = br.readLine()) != null) {
				sb.append(output);
			}
			conn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return String.valueOf(sb);
	}
	
	/**
	 * 向server端发送请求,并获取返回参数
	 * 
	 * @param actionUrl
	 * @param method
	 * @param param
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static  String sendGetRequest(String targetURL) throws UnsupportedEncodingException{
		logger.debug("+++++++++++++++sendGetRequest方法开始!+++++++++++++++++++");
		String output = "";
		String outStr = "";
		try {
			URL restServiceURL = new URL(targetURL);
			HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
			httpConnection.setRequestMethod("GET");
			httpConnection.setRequestProperty("Charset", "utf-8");
			httpConnection.setRequestProperty("Content-Type", "application/json");
			
			//解决乱码代码
			httpConnection.setRequestProperty("Accept-Charset", "utf-8");
			httpConnection.setRequestProperty("Accept-Encoding", "utf-8");
			
			if (httpConnection.getResponseCode() != 200) {
				throw new RuntimeException("HTTP GET Request Failed with Error code : " + httpConnection.getResponseCode());
			}
			BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), "utf-8"));
			while ((output = responseBuffer.readLine()) != null) {
				outStr = outStr + output;
			}
			httpConnection.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			logger.error("io Exception:see code");
			e.printStackTrace();
		}
		return outStr;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~~Whd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值