3

I tried this way to send HTTP POST request: Java - sending HTTP parameters via POST method easily

But the problem is: what if I need to connect to a specified port number like 8080? Cause if I do

String request = "http://example.com:8080/index.php";

It gives me

java.net.ConnectException: Operation timed out

clemens
  • 16,716
  • 11
  • 50
  • 65
Cacheing
  • 3,431
  • 20
  • 46
  • 65
  • 3
    Then that is probably not the port your service is deployed to. – Sotirios Delimanolis Oct 14 '13 at 19:59
  • 1
    @SotiriosDelimanolis So this is the right way to call a service with specified port number? I mean, append it to the url? – Cacheing Oct 14 '13 at 20:01
  • Can you connect to the url with our browser? Maybe issue a http post with plugins such as [Advanced REST client](https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo) or [REST Client](http://restclient.net/) to see if it is responding correctly from the same machine that you are trying to connect with Java. – Anthony Accioly Oct 14 '13 at 20:03
  • 1
    @Cacheing, yes it is (But you should post relevant parts of your code so that we can take a look). – Anthony Accioly Oct 14 '13 at 20:04
  • @AnthonyAccioly what is strange is that I can send the request using Dev HTTP client, but I cannot do that using code. – Cacheing Oct 14 '13 at 20:04
  • Same machine? Same http headers? – Anthony Accioly Oct 14 '13 at 20:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39212/discussion-between-anthony-accioly-and-cacheing) – Anthony Accioly Oct 14 '13 at 20:07
  • How do you construct your URL ? if you want to pass the port you should use following constructor: URL(String protocol, String host, int port, String file) – Frederic Close Oct 14 '13 at 20:07
  • duplicaet of https://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily – tkruse Jan 15 '18 at 03:35

1 Answers1

0

You probably need to connect to that port number because the service you want to access is listening on that port. The attempt to add the port number to the URL as you did is correct for such cases.

Now you may get a 'Connection refused'. This can happen if there is no process listening on the other side to accept the connection. It might even mean the service that is running is not interested in your connection. Remember some server processes can be configured to only accept connections with specific limitations.

You may also get something like 'No route to host', 'Unknown hostname' or stuff that indicates network problems. Another such network problem is a connection timeout, which today usually points towards a firewall denying access.

To summarize: Be aware you got a Connectexception. That means the TCP connection was not established - at that point in time HTTP was not started, and it does not matter whether you intend to GET or POST. I think you did well on the client side, maybe you need to check the server side why it does not accept your incoming connection.

Queeg
  • 7,748
  • 1
  • 16
  • 42