1

I would like to do a request with this statement of curl:

curl -u Username:Password -d "auth" https://.../.../...

I want create a connection to battle.net (Blizzard developer's API) but I don't know hot to implement -d. I hope someone can help me. Sry for my bad english. I am new in this community.

    public static void main(String[] args) throws IOException {

        String user = "...";
        String pwd = "...";

        try {
          URL url = new URL ("https://(battle.net)");
          String encoding = Base64.getEncoder().encodeToString((user + ":" + pwd).getBytes("UTF-8"));

          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
          connection.setRequestMethod("POST");
          connection.setDoOutput(true);
          connection.setRequestProperty  ("authorization_code", encoding);
          InputStream content = (InputStream)connection.getInputStream();
          BufferedReader in   = 
              new BufferedReader (new InputStreamReader (content));
          String line;
          while ((line = in.readLine()) != null) {
              System.out.println(line);
          }
      } catch(Exception e) {
          e.printStackTrace();
      }

    }

ERROR:

java.io.IOException: Server returned HTTP response code: 401 for URL: "https://(battle.net)"
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at gnfcxfg.asd.main(asd.java:28)
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
WOWBubbi97
  • 67
  • 1
  • 7

1 Answers1

0

According to -d, --data <data> on curl's man page:

This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.

I'd set:

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

URLConnection has getOutputStream():

Returns an output stream that writes to this connection.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • thanks a lot. how can I output this outputStream? `connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty ("authorization_code", encoding); connection.setRequestProperty ("grant_type", "authorization_code"); OutputStream out = (OutputStream) connection.getOutputStream();` – WOWBubbi97 Sep 30 '19 at 20:37
  • @WOWBubbi97 [out.write(...)](https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html). The cast `(OutputStream)` shouldn't be necessary since `getOutputStream()` returns exactly that. – Gerold Broser Sep 30 '19 at 20:44
  • sry for my dumb question but what i do next with the outputstream ? Have I send a json ? – WOWBubbi97 Sep 30 '19 at 20:54
  • @WOWBubbi97 You write to it what you would give as `-d` argument to `curl`. See [Write string to output stream](https://stackoverflow.com/q/4069028/1744774). – Gerold Broser Sep 30 '19 at 21:17
  • I thank you very lot. I have found a example and now I get my Token. Is it ok to look in other code? I feel me so dumb :( – WOWBubbi97 Sep 30 '19 at 21:24
  • @WOWBubbi97 You're welcome. If the code is freely available it's absolutely OK to look at it. That's, for instance, one of the ideas behind Open Source. No need for feeling dumb. We all were beginners once and at everything. BTW, you can give an answerer some extra credit if you not only accept the answer but also upvote it, see [Why is voting important?](https://stackoverflow.com/help/why-vote). – Gerold Broser Oct 05 '19 at 09:28