java api实现http请求

java api实现,无需导入其他包

1、

public static String httpRequestUtils(String url, String params){
                PrintWriter out = null;  
	        BufferedReader in = null;  
	        StringBuilder result = new StringBuilder(); 
	        try {  
	            URL reqUrl = new URL(url);  
	            // 建立连接
	            URLConnection conn = reqUrl.openConnection();  
	            
	            //设置请求头
	            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); 
	//          conn.setRequestProperty("Connection", "Keep-Alive");//保持长连接
	            conn.setDoOutput(true); //设置为true才可以使用conn.getOutputStream().write()
	            conn.setDoInput(true); //才可以使用conn.getInputStream().read();
	            
	            //写入参数
	            out = new PrintWriter(conn.getOutputStream()); 
	            //设置参数,可以直接写&参数,也可以直接传入拼接好的
	            out.print(params);
	            // flush输出流的缓冲  
	            out.flush();  
	            
	            // 定义BufferedReader输入流来读取URL的响应  
	            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));  
	            String line;  
	            while ((line = in.readLine()) != null) {  
	                result.append(line);  
	            }  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }finally {// 使用finally块来关闭输出流、输入流  
	            try {  
	                if (out != null) {  
	                    out.close();  
	                }  
	                if (in != null) {  
	                    in.close();  
	                }  
	            } catch (IOException ex) {  
	                ex.printStackTrace();  
	            }  
	        }  
	       return result.toString();
       }

2、main方法测试:

	    public static void main(String [] args){
	    	String httpurl = "https://api.weixin.qq.com";//接口名
	    	String params = "&a=1&b=2";//如此拼接即可
	    	String result = httpRequestUtils(httpurl, params);
	    	System.out.println("result:" + result);
	    }

 

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,你可以使用`java.net.HttpURLConnection`类或者更高级的库如`Apache HttpClient`来实现HTTP请求。下面是使用`java.net.HttpURLConnection`类发送GET请求的示例: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Main { public static void main(String[] args) { try { // 创建URL对象 URL url = new URL("https://api.example.com/endpoint"); // 打开连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法 connection.setRequestMethod("GET"); // 发送请求并获取响应代码 int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode); // 读取响应内容 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); // 输出响应内容 System.out.println("Response: " + response.toString()); // 关闭连接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述示例演示了如何使用`java.net.HttpURLConnection`类发送GET请求,并获取响应内容。你可以根据需求修改请求方法、URL以及其他参数来实现不同类型的请求。 还可以使用第三方库如`Apache HttpClient`来简化HTTP请求的处理。你可以使用Maven或Gradle将其添加到项目依赖中,并使用其提供的API进行HTTP请求。以下是使用`Apache HttpClient`库发送GET请求的示例: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class Main { public static void main(String[] args) { try { // 创建HttpClient对象 HttpClient httpClient = HttpClientBuilder.create().build(); // 创建HttpGet对象,并设置URL HttpGet httpGet = new HttpGet("https://api.example.com/endpoint"); // 发送请求并获取响应 HttpResponse response = httpClient.execute(httpGet); // 获取响应实体 HttpEntity entity = response.getEntity(); // 读取响应内容 String responseString = EntityUtils.toString(entity, "UTF-8"); // 输出响应内容 System.out.println("Response: " + responseString); } catch (Exception e) { e.printStackTrace(); } } } ``` 使用`Apache HttpClient`库可以更方便地处理HTTP请求和响应。你可以根据项目需求配置请求参数、处理响应数据等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值