在Java中,使用GET请求处理XML参数并返回结果

在Java中,要使用GET请求处理XML参数并返回结果,你可以使用HttpURLConnection或更现代化的Apache HttpClient库。下面是两个示例:

  1. 使用HttpURLConnection:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    
    public class XMLGetRequest {
    
        public static Document getXMLFromUrl(String url, String xmlData) {
            try {
                // 创建URL
                URL urlObject = new URL(url);
    
                // 创建连接
                HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection();
                // 设置为GET请求
                connection.setRequestMethod("GET");
    
                // 添加XML数据为查询参数
                String encodedData = escapeXML(xmlData);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestProperty("Content-Length", Integer.toString(xmlData.length()));
                connection.getOutputStream().write(encodedData.getBytes());
    
                // 设置无连接,防止阻塞
                connection.setUseCaches(false);
                connection.setDoInput(true);
                connection.setDoOutput(true);
    
                // 读取响应
                int responseCode = connection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    StringBuilder response = new StringBuilder();
                    String inputLine;
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
    
                    // 解析XML响应
                    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                    Document doc = dBuilder.parse(new InputSource(new StringReader(response.toString())));
                    return doc;
    
                } else {
                    throw new RuntimeException("Failed to connect with response code: " + responseCode);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        // 用于URL编码XML数据
        private static String escapeXML(String xmlData) {
            try {
                return new String(xmlData.getBytes("UTF-8"), "UTF-8");
            } catch (Exception e) {
                throw new RuntimeException("Error escaping XML data", e);
            }
        }
    }
  2. 使用Apache HttpClient:

    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.w3c.dom.Document;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    
    public class XMLGetRequestWithApache {
    
        public static Document getXMLFromUrl(String url, String xmlData) {
            try {
                // 创建HttpClient实例
                CloseableHttpClient httpClient = HttpClients.createDefault();
    
                // 创建GET请求
                HttpGet httpGet = new HttpGet(url);
    
                // 设置查询参数
                StringEntity entity = new StringEntity(xmlData);
                entity.setContentType("application/x-www-form-urlencoded");
                httpGet.setEntity(entity);
    
                // 执行请求
                CloseableHttpResponse response = httpClient.execute(httpGet);
    
                // 确保请求成功
                if (response.getStatusLine().getStatusCode() == 200) {
                    String responseBody = EntityUtils.toString(response.getEntity());
                    // 解析XML响应
                    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                    Document doc = dBuilder.parse(new InputSource(new StringReader(responseBody)));
                    return doc;
                } else {
                    throw new RuntimeException("Failed to connect with response code: " + response.getStatusLine().getStatusCode());
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                // 关闭连接
                try {
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException ioex) {
                    ioex.printStackTrace();
                }
            }
        }
    }

    如果你想返回的是字符串类型,你可以直接从HTTP响应中读取并解析XML。以下是两个示例中的修改版本,返回结果是一个字符串:

  3. 使用HttpURLConnection,返回字符串:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class XMLGetRequest {
    
        public static String getXMLFromUrl(String url, String xmlData) {
            try {
                // 创建URL
                URL urlObject = new URL(url);
    
                // 创建连接
                HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection();
                // 设置为GET请求
                connection.setRequestMethod("GET");
    
                // 添加XML数据为查询参数
                String encodedData = escapeXML(xmlData);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestProperty("Content-Length", Integer.toString(xmlData.length()));
                connection.getOutputStream().write(encodedData.getBytes());
    
                // 设置无连接,防止阻塞
                connection.setUseCaches(false);
                connection.setDoInput(true);
                connection.setDoOutput(true);
    
                // 读取响应
                int responseCode = connection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    StringBuilder response = new StringBuilder();
                    String inputLine;
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
    
                    return response.toString();
                } else {
                    throw new RuntimeException("Failed to connect with response code: " + responseCode);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        // 用于URL编码XML数据
        private static String escapeXML(String xmlData) {
            // (代码不变,此处省略)
        }
    }
  4. 使用Apache HttpClient,返回字符串:

    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    
    public class XMLGetRequestWithApache {
    
        public static String getXMLFromUrl(String url, String xmlData) {
            try {
                // 创建HttpClient实例
                CloseableHttpClient httpClient = HttpClients.createDefault();
    
                // 创建GET请求
                HttpGet httpGet = new HttpGet(url);
    
                // 设置查询参数
                StringEntity entity = new StringEntity(xmlData);
                entity.setContentType("application/x-www-form-urlencoded");
                httpGet.setEntity(entity);
    
                // 执行请求
                CloseableHttpResponse response = httpClient.execute(httpGet);
    
                // 确保请求成功
                if (response.getStatusLine().getStatusCode() == 200) {
                    return EntityUtils.toString(response.getEntity());
                } else {
                    throw new RuntimeException("Failed to connect with response code: " + response.getStatusLine().getStatusCode());
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                // 关闭连接
                try {
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException ioex) {
                    ioex.printStackTrace();
                }
            }
        }
    }

    这两个示例现在都会返回一个字符串类型的XML响应内容。如果需要解析XML,你可以使用javax.xml.parsers包的DocumentBuilder或相关的XML解析库,如DOM4J, JAXB, Jackson-dataformat-xml等。
    在使用Apache HttpClient时,HttpGet对象不能直接设置实体(entity),因为HttpGet是用于发送GET请求的,并不携带请求体。你应该用HttpGet构建URL并传递 parameters,而不是设置实体。以下是改进后的版本:

  5. 使用 HttpUrl encodedGet 方法来构造GET请求:

    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    public class XMLGetRequestWithApache {
    
        public static String getXMLFromUrl(String url, String xmlData) {
            try {
                // 创建HttpClient实例
                CloseableHttpClient httpClient = HttpClients.createDefault();
    
                // 创建包含查询参数的GET请求
                HttpGet httpGet = new HttpGet(url + "?" + xmlData);
    
                // 执行请求
                CloseableHttpResponse response = httpClient.execute(httpGet);
    
                // 确保请求成功
                if (response.getStatusLine().getStatusCode() == 200) {
                    return EntityUtils.toString(response.getEntity());
                } else {
                    throw new RuntimeException("Failed to connect with response code: " + response.getStatusLine().getStatusCode());
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                // 关闭连接
                try {
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException ioex) {
                    ioex.printStackTrace();
                }
            }
        }
    }

    在这个版本中,我们在URL上添加了查询参数,形式为 url?xmlData,而不是使用 setEntity。请注意,这种方式可能不适用于非常大的查询参数,因为URL长度有一定的限制。
    以上代码报错:java.lang.IllegalArgumentException: Illegal character in query at index 24: http://....?<result>...</result>

    字符编码问题或者某些字符需要URL编码。在HTTP查询参数中,有一些字符(如空格、特殊字符等)需要转换为它们的URL编码形式,如 %20 代表空格。

    在上面的代码中,添加查询参数时没有进行编码。让我们对 xmlData 使用 URLEncoder 进行编码,然后再构造 HttpGet。以下是修改后的代码:

  6. 使用URLEncoder进行编码

    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import java.net.URLEncoder;
    import java.nio.charset.StandardCharsets;
    
    public class XMLGetRequestWithApache {
    
        public static String getXMLFromUrl(String url, String xmlData) {
            try {
                // 创建编码后的查询参数
                String encodedData = URLEncoder.encode(xmlData, StandardCharsets.UTF_8).replace("+", "%20");
    
                // 创建HttpClient实例
                CloseableHttpClient httpClient = HttpClients.createDefault();
    
                // 创建带有编码查询参数的GET请求
                HttpGet httpGet = new HttpGet(url + "?" + encodedData);
    
                // 执行请求
                CloseableHttpResponse response = httpClient.execute(httpGet);
    
                // 确保请求成功
                if (response.getStatusLine().getStatusCode() == 200) {
                    return EntityUtils.toString(response.getEntity());
                } else {
                    throw new RuntimeException("Failed to connect with response code: " + response.getStatusLine().getStatusCode());
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                // 关闭连接
                try {
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException ioex) {
                    ioex.printStackTrace();
                }
            }
        }
    }

    现在,xmlData 字符串将被正确编码,并用于构造GET请求的URL。如果问题仍未解决,可能是有字符在URL编码中是不被接受的,可能需要根据服务器的要求进一步调整。如果你确认URL编码后返回的结果仍然有错误,可能是因为服务器端接收到的编码并不支持或期望的不是标准的URL编码格式。

  7. 使用post请求

    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import java.io.IOException;
    
    public class XMLPostRequestWithApache {
    
        public static String postXMLToUrl(String url, String xmlData) {
            try {
                // 创建HttpClient实例
                CloseableHttpClient httpClient = HttpClients.createDefault();
    
                // 创建POST请求
                HttpPost httpPost = new HttpPost(url);
    
                // 将请求体设为XML数据
                httpPost.setEntity(new StringEntity(xmlData, StandardCharsets.UTF_8));
    
                // 设置适当的请求头,视情况可能需要添加如Content-Type等
                httpPost.setHeader("Content-Type", "text/xml");
    
                // 发送请求并获取响应
                CloseableHttpResponse response = httpClient.execute(httpPost);
    
                // 确保请求成功
                if (response.getStatusLine().getStatusCode() == 200) {
                    String responseBody = EntityUtils.toString(response.getEntity());
                    return responseBody;
                } else {
                    throw new RuntimeException("Failed to connect with response code: " + response.getStatusLine().getStatusCode());
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                // 关闭连接
                try {
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException ioex) {
                    ioex.printStackTrace();
                }
            }
        }
    }

    在这个示例中,我使用了 HttpPost 发布POST请求,设置了请求体(StringEntity),并管理了请求头。你需要确保Content-Type符合服务器的要求,如这里是text/xml。如果你知道服务器端需要的HTTP方法(如 PATCHPUT 等),请相应地替换 HttpPost

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 后端,可以使用 Spring MVC 框架来接收 GET 请求参数。如果 GET 请求参数为对象,可以使用 @RequestParam 注解来接收参数,然后使用 Jackson 库将参数转换为对象。具体的实现步骤如下: 1. 添加 Jackson 库的依赖 在 pom.xml 文件添加以下依赖: ```xml <!-- Jackson 库 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency> ``` 2. 在 Controller 接收参数 在 Controller 使用 @RequestParam 注解来接收参数,例如: ```java @GetMapping("/api/user") public String getUser(@RequestParam("params") String params) { ObjectMapper mapper = new ObjectMapper(); try { User user = mapper.readValue(params, User.class); // 处理请求参数 // ... } catch (JsonProcessingException e) { e.printStackTrace(); } return "success"; } ``` 其,@RequestParam("params") 表示接收名为 "params" 的参数。Jackson 库的 ObjectMapper 类可以将字符串转换为指定的对象,例如上面的代码将字符串 params 转换为 User 类型的对象。 3. 定义 User 类 定义一个 User 类,用于存储接收到的参数。例如: ```java public class User { private String name; private int age; // 省略 getter 和 setter 方法 } ``` 4. 发送 GET 请求 在前端发送 GET 请求时,需要将传递的参数序列化为字符串,并将字符串作为参数的值传递。例如: ```javascript var user = {name: "Tom", age: 18}; var params = JSON.stringify(user); var url = "/api/user?params=" + params; fetch(url).then(response => { // 处理响应结果 // ... }); ``` 在上面的代码使用了 JSON.stringify() 方法将对象 user 序列化为字符串,然后将字符串作为参数的值传递给后端。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值