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:[email protected]/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?

11
  • I forgot to mention, it is within a try and catch but no exceptions come up. Commented Aug 20, 2014 at 19:50
  • What response do you get? Commented Aug 20, 2014 at 19:52
  • 1
    There's a getResponseCode method. You can also getInputStream or getErrorStream to see if there was a response body. Commented Aug 20, 2014 at 19:55
  • 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. Commented Aug 20, 2014 at 19:58
  • 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. Commented Aug 20, 2014 at 20:04

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.