aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes http posting using Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "http posting using "HttpUrlConnection Watch "http posting using "HttpUrlConnection New topic
Author

http posting using "HttpUrlConnection'

J Virumbi
Greenhorn

Joined: Nov 15, 2003
Posts: 3
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();}
}
}


JVirumbi
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12778
    
    5
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

Joined: Mar 09, 2001
Posts: 2676
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.


Matthew Phillips
J Virumbi
Greenhorn

Joined: Nov 15, 2003
Posts: 3
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

Joined: Feb 14, 2002
Posts: 100
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

Joined: Mar 22, 2000
Posts: 12778
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: http posting using "HttpUrlConnection'