具有参数的Http GET请求-Java

时间:2019-01-15 19:08:15

标签: java

我需要创建一个Java代码,该代码发送带有所需参数的Http GET请求以获取响应,否则我将收到一条缺少该参数的信息消息。目前,我有第二种选择。

为什么我的DataOutputStream无法在请求中写入参数?

查看代码:

public class ClienteWebService {
String URL_WEBSERVICE = "http://seat.ind.br/processo-seletivo/2018/01/desafio.php";
String nome = "Leonardo Rodrigues Torres";


public String getInformacoes() {

    StringBuilder urlParametros = new StringBuilder();
    urlParametros.append("nome=").append(nome);

    URL url;
    try {
        url = new URL(URL_WEBSERVICE);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(urlParametros.toString());
        wr.flush();
        wr.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();


        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        return response.toString();



    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
  }

}

0 个答案:

没有答案