1

I'm trying to make a simple http post using the apache http client, and I can't for the life of me get it to work. I'm basing this off of the example at: http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpState.html . The reason I don't think it's a problem with my url, host, or port is because I got them all to work in regular html forms and also in telnet.

The error I get is:

org.apache.commons.httpclient.ProtocolException: The server somehost.something.com failed to respond with a valid HTTP response

Here is my code:

public InputStream trialPost() { PostMethod post = new PostMethod("https://somesite.com/example.php"); NameValuePair[] data = { new NameValuePair("parameter1", "blah"), new NameValuePair("parameter2", "blah") }; post.setRequestBody(data); HttpState hs = new HttpState(); HttpConnection hc = new HttpConnection("somehost.something.com", 443); InputStream in = null; try { hc.open(); post.execute(hs, hc); in = post.getResponseBodyAsStream(); hc.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // handle response. return in; } 
9
  • Define "does not work". Error message? Timeout? Hangs? Commented Nov 28, 2012 at 22:27
  • I just edited my question with the error message. Commented Nov 28, 2012 at 22:28
  • 2
    While I'm not an expert in this field, my guess is that you're doing an HTTP Post but your URL is HTTPS. And that's why the server "failed to respond with a valid HTTP response" because it was an HTTPS response. Commented Nov 28, 2012 at 22:31
  • You are not receiving a valid HEADER response. Are you sure you are even reaching the intended page? Commented Nov 28, 2012 at 22:31
  • @unexpected Do I need to add somewhere that I didn't already that it's https? I used port # 443, and the url in question has https in it. Not sure if I need to include it somewhere else also. Commented Nov 28, 2012 at 22:33

1 Answer 1

2

You are trying to use an HttpConnection on a server using HTTPS which won't work. I suggest you take a look at the HTTPClient docs on communicating over SSL.

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

2 Comments

This sounds promising...trying to implement your solution now.
With that said, also consider that the host must allow external posting via SSL. Some hosts may consider this a security issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.