1

当我尝试向我的服务器发送请求时,无论我是否设置 setRequestMethod("GET") ,服务器都只会收到 POST 请求。这是我正在调用的函数,带有我需要与它们一起发送的 url 和 2 个参数:

public static String getHTML(String urlToRead,String urlParameters) {
    try {
        StringBuilder result = new StringBuilder();
        URL url = new URL(urlToRead);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));

        conn.setUseCaches(false);
        conn.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream (
            conn.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.close();

            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }

            rd.close();
            return result.toString();
        }
        catch (Exception e) {
            return e.getMessage().toString();
        }
    }
}

欢迎任何帮助或任何其他能够通过发送 URL 和两个参数向服务器发送 GET 请求的功能。

4

1 回答 1

1

执行以下操作:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

class Main {
    public static void main(String[] args) {
        System.out.println(getHTML("http://localhost:8080/TestDynamicProject/getdata.do?t1=tp1&t2=tp2"));
    }

    public static String getHTML(String urlToRead) {
        try {
            StringBuilder result = new StringBuilder();
            InputStream stream = null;
            URL url = new URL(urlToRead);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.connect();
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = conn.getInputStream();
            }

            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }

            rd.close();
            return result.toString();
        } catch (Exception e) {
            return e.getMessage().toString();
        }
    }
}

输出:

Response from GET | Request parameters: t1=[tp1],t2=[tp2]

下面给出的是我的Servlet

@WebServlet("/getdata.do")
public class TestServlet extends HttpServlet {

    public TestServlet() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        StringBuilder sb = new StringBuilder();
        request.getParameterMap().forEach((k, v) -> sb.append(k.toString() + "=" + Arrays.toString(v) + ","));
        sb.deleteCharAt(sb.length() - 1);
        response.getWriter().append("Response from GET").append(" | Request parameters: ").append(sb);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Response from POST");
    }
}
于 2019-12-14T19:07:07.957 回答