java 之 模拟http请求

本文提供了一个使用 Java 实现的 HttpClient 类示例,展示了如何通过 GET 和 POST 方法发起 HTTP 请求,并处理响应。该示例包括设置连接超时、请求头、请求参数等关键步骤。

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;


public class BasicHttpClient {
	private static final int TIME_OUT = 1000*6;//超时
	private static final String METHOD_POST = "POST";
	private static final String METHOD_GET = "GET";
	private static final int HTTP_OK = 200;
	
	public String httpGet(String urlStr) {
		URL url = null;
		HttpURLConnection conn = null;
		InputStream inStream = null;
		String response = null;
		
		try {
			url = new URL(urlStr);
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.setConnectTimeout(TIME_OUT);
			conn.setRequestMethod(METHOD_GET);
			conn.setRequestProperty("accept", "*/*");
			conn.connect();
			
			int responseCode = conn.getResponseCode();
			if(responseCode==HTTP_OK) {
				inStream = conn.getInputStream();
				response = getResponse(inStream);
				
			} else {
				response = "返回码:"+responseCode;
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			conn.disconnect();
		}
		return response;
	}

	public String httpPost(String urlStr,String params) {
		byte[] data = params.getBytes();
		URL url = null;
		HttpURLConnection conn = null;
		String response = null;
		InputStream inStream = null;
		try {
			url = new URL(urlStr);
			conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(TIME_OUT);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod(METHOD_POST);
			
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("Charset", "UTF-8");
			conn.setRequestProperty("Content-Length", String.valueOf(data.length));
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			conn.connect();
			
			DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
			//把参数传过去server
			outputStream.write(data);
			outputStream.flush();
			outputStream.close();
			
			int responseCode = conn.getResponseCode();
			if(responseCode==HTTP_OK) {
				inStream = conn.getInputStream();
				response = getResponse(inStream);
			} else {
				response = "返回码:"+responseCode;
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			conn.disconnect();
		}
		return  response;
	}
	private String getResponse(InputStream inStream) throws IOException {
		// TODO Auto-generated method stub
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		int len = -1;
		byte[] buffer = new byte[1024];
		while((len=inStream.read(buffer))!=-1) {
			outputStream.write(buffer,0,len);
		}
		byte[] data = outputStream.toByteArray();
		return new String(data);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值