0

I'm trying to get HttpURLConnection to post a JSON string to a PHP file remotely. It's not working no matter what I do. This is my current code:

HttpURLConnection httpcon = (HttpURLConnection) ((new URL('http://domain.com/me.php').openConnection()));
                            httpcon.setDoOutput(true);
                            httpcon.setRequestProperty("Content-Type", "application/json");
                            httpcon.setRequestProperty("Accept", "application/json");
                            httpcon.setRequestMethod("POST");
                            httpcon.connect();

                            String initial = "{'out': '" + idir + prod + ".jpg', 'in': '" + item[3] + "'}";
                            byte[] outputBytes = initial.getBytes("UTF-8");

                            OutputStream os = httpcon.getOutputStream();
                            os.write(outputBytes);

                            os.close();

I know that the initial string contains data, I have run a System.out.println on it and the outputBytes variables and both have contents.

I know it's not posting because I have the PHP file set to save posted contents to a file locally. No file is ever created when running it.

I know the PHP side of things work and the server accepts posts as I can run this just fine:

$ curl -H "Accept: application/json" -H "Content-Type: application/json" -d "{'value': 7.5}" "http://domain.com/me.php"

And it works fine creating the file and writing post content to output file.

EDIT

Ok, after printing the responses I'm now getting a 401 code, unauthorized. I'm using Apache HTPASSWD authentication, but I was passing the user and pass in the URL as http://user:pass@domain.com/me.php. This worked from curl from the CLI, so I thought it would work here also, evidently it does not.

So how do I authenticate using HttpURLConnection?

jfreak53
  • 2,239
  • 7
  • 37
  • 53
  • I forgot to mention, it is within a `try` and `catch` but no exceptions come up. – jfreak53 Aug 20 '14 at 19:50
  • What response do you get? – Sotirios Delimanolis Aug 20 '14 at 19:52
  • I don't think the code is grabbing a response and printing it. How would I print a response so I can post here? – jfreak53 Aug 20 '14 at 19:52
  • 1
    There's a `getResponseCode` method. You can also `getInputStream` or `getErrorStream` to see if there was a response body. – Sotirios Delimanolis Aug 20 '14 at 19:55
  • This might be useless... but, how do you know that the PHP parses well your JSON when you are passing another JSON object to test it? – luanjot Aug 20 '14 at 19:56
  • 1
    @luanjot At this point it doesn't matter wether PHP parses correctly or not. The fact of the matter is PHP isn't even creating the file on calling it from a url. The file write method of PHP is called at the very top before anything, heck I can call the PHP file from a URL bar in a browser and get an empty file created. So the fact is that no matter what PHP is doing Java is never making a connection to the file, hence where the problem lies. – jfreak53 Aug 20 '14 at 19:58
  • @SotiriosDelimanolis Ok, I'm getting a 401 code, unauthorized. I'm using Apache HTPASSWD authentication, but I was passing the user and pass in the URL as `http://user:pass@domain.com/me.php`. This works from curl from the CLI, so I thought it would work here also, evidently it does not. So how do I authenticate using `HttpURLConnection`? – jfreak53 Aug 20 '14 at 20:03
  • 1
    You should update your question with those details. I don't know how but I'm sure there are some related questions/answers around here. – Sotirios Delimanolis Aug 20 '14 at 20:04
  • can you add this header in your PHP: "Access-Control-Allow-Origin: *" – NV Bhargava Aug 20 '14 at 20:05
  • @NVBhargava No, that doesn't fix it, sorry. – jfreak53 Aug 20 '14 at 20:07
  • Try using an authenticator in your Java class: http://stackoverflow.com/questions/3283234/http-basic-authentication-in-java-using-httpclient – luanjot Aug 20 '14 at 20:08

1 Answers1

1

You need to set up basic authentication. You can do it through plain Java or using Apache HttpClient.

How to handle HTTP authentication using HttpURLConnection?

http://www.baeldung.com/httpclient-4-basic-authentication

Community
  • 1
  • 1
markbernard
  • 1,412
  • 9
  • 18