File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

http posting using "HttpUrlConnection'

 
J Virumbi
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I want to call some web URL from java application.
When I use "HttpUrlConnection", I get an "OK" responce message. But the target webserver is NOT processing the request appropriately. (I can not check the code in the target app)
I mean,
If u use the same URL straight in the browser, It is working fine as expected.
is there any header prop, etc... or anything else need to be set in for the HttpUrlConnection class.
(Additional question:How to provide the proxy user_name and pass_word for the UrlConn ??)
Fowwing the URL and the code
++++++++++++++++++++++++++++++
import java.net.*;
import java.io.*;
public class HttpTest
{
public static void main (String[] s)
{
try{
URL url = new URL("http://www.m1.com.sg/msgcenter/handphone/msg.cgi?type=handphone&pno=12345678&msg=testmsg");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();

connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.usingProxy();
java.util.Properties props = System.getProperties();
props.put("http.useProxy", "true");
props.put("http.proxyHost", "33.33.3.3");
props.put("http.proxyPort", "8080");
connection.connect();

System.out.println("\n Responce Msg: "+connection.getResponseMessage() );

System.out.println("\n M1 Server Date: "+connection.getDate());
System.out.println("\n Local Sys Date: "+System.currentTimeMillis());

connection.disconnect();

}catch(Exception e){e.printStackTrace();}
}
}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13044
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use the URL directly in a browser, you are doing a GET, not a POST - exactly why do you use POST?
Bill
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't look like your code has anything to do with web services. I'll move this to the Sockets and Internet Protocols forum for you.
 
J Virumbi
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
1) I tried both GET as well as POST. It did not work.
2) is it possible for a webserver to know wheather a http request received by it has been made from a browser or from an application ?
3) is that a right piece of code for what I am looking for ?
Thanks
 
Rishi Tyagi
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just try the status code
connection.getResponseCode() method can be used for the same, and check the status of your request, Hope it will help somewhere
Rishi
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13044
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. What exactly are you expecting the target webserver to do? GET and POST are often used for completely separate purposes. What are you using as your documentation on how to call the service?
2. No - all a service has to go on is the headers and your application can set the headers.
You don't seem to be looking at anything except the response message - why?
Bill
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic