java 拼接http请求

本文介绍了一个简单的Java程序,用于通过GET请求从指定URL获取数据。程序展示了如何建立连接、设置请求头并读取服务器响应。

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

public static String sendForServer(String vip_business_code,String url){  
		String url2 = EL_URL+url;
		System.out.println(url+"======================"+url2);
		String result = "";
        BufferedReader in = null;
        try {
           
            URL realUrl = new URL(url2);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    } 

Java中,如果你已经拼接好了HTTP请求(通常是URL、参数等),你可以使用`java.net.HttpURLConnection`或者更现代的第三方库如`org.apache.httpclient`、`okhttp` 或 `retrofit` 来发送请求。这里是一个基本的例子: ```java import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class HttpRequestExample { public static void main(String[] args) throws Exception { String url = "http://example.com/api/resource"; String params = "param1=value1&param2=value2"; // 创建URL对象 URL obj = new URL(url + "?" + params); // 打开连接 HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置请求方法(GET或POST) con.setRequestMethod("GET"); // 添加请求头信息(如果需要身份验证或其他头部信息) con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("Authorization", "Bearer your_token"); // 假设这里有API令牌 // 如果是POST请求,可以设置输入流 if ("POST".equals(con.getRequestMethod())) { byte[] data = params.getBytes(); con.setDoOutput(true); OutputStream os = con.getOutputStream(); os.write(data); os.flush(); os.close(); } // 获取响应状态码 int responseCode = con.getResponseCode(); System.out.println("Response Code : " + responseCode); // 读取响应数据 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 输出结果 System.out.println(response.toString()); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值