We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


J2ME HTTP POST not working with static IP address

dgbweb asked
Medium Priority
Last Modified: 2007-11-27

Anyone know why a website would return a 404 error when you specify the IP address rather than the domain name. Both a JSP and a ASP website give me the same response.

The code snippet is below.

// these params work fine
String url = "http://localhost:8080/WebModule2/jsp1.jsp";
String formData = "username=foobar&password=foobar&Submit=Submit";

// these params work fine also
String url = "http://mydomainname.com/LoginProc.jsp";
String formData = "username=foobar&password=foobar&Submit=Submit";

//these params generate a 404 Not Found response from the server
// where the IP used is that reported when you ping the domain name
String url = "";
String formData = "username=foobar&password=foobar&Submit=Submit";

The code is as follows:

HttpConnection c = (HttpConnection)Connector.open(url, Connector.READ_WRITE, true);
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String clen = Integer.toString(formData.length());
c.setRequestProperty("Content-length", clen);                              
OutputStream os = c.openOutputStream();
Watch Question

Have you tried specifying the port number for that IP?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


yes, I tried appending ":80" to the ip address. Didn't help with either site.

any other ideas?

This is an interesting issue. From a MIDP perspective, there is nothing that should prevent the use of an IP address rather than a hostname.
Does the website respond when you bring it up via IP address in a browser?

Also, does this happen in the emulator, just on the device, or both?


I am just on the emulator at present (sun's wireless toolkit with JBuilder2006). I suppose it is worth a try to download it to the phone and see if it works there or not.

the target page is known to work fine ( a production app is calling it). The browser works fine too.

the same code using the GET method returns an application specific error but at least it indicates the server found the page.

I am getting an html formatted response from the server with the 404 error code and the message that the server cannot find the specified file "/LoginProc.jsp".

If it isn't much work, maybe one of you could cut/paste the code into one of your apps and see what happens in your emulators.

"the same code using the GET method returns an application specific error but at least it indicates the server found the page"

That's a good clue -

Get the response code from the request and maybe that will lead to a better clue:

// after this line

// get the response code
int code = c.getResponseCode();
System.out.println("RESPONSE CODE: " + Integer.toString(code));

Maybe it's throwing back 411 (invalid content-length) or something like that -- at least, we can hope it's a better clue...



the problem turned out to be a combination of needing to append ":80" to a fixed IP address, and setting the content length request property.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.