Skip to main content

Regarding HttpUrl Connection

5 replies [Last post]
selvarevathi
Offline
Joined: 2007-01-29

hi,
wen im trying to execute this code with
con.setRequestMethod("GET"); method it works fine
where as if put POST its throwing exception
The code as follows

HttpURLConnection con = null;
URL urlnew = new URL (url);
con = (HttpURLConnection)urlnew.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
con.setRequestProperty("Content-type","text/html");
con.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
response=in.readLine();
in.close();

The Exception Trace are as follows

2007-08-02 12:56:44,833 INFO STDOUT - java.io.IOException: Server returned HTTP response code: 411 for URL: http://192.168.1.60:8080/gmt/hitWipAction?username=meruodc&password=meru...
2007-08-02 12:56:44,833 INFO STDOUT - at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:800)
2007-08-02 12:56:44,833 INFO STDOUT - at com.meru.gmt.classes.urlHitter.hitUrl(urlHitter.java:56)
2007-08-02 12:56:44,833 INFO STDOUT - at com.meru.gmt.bean.message.requestReader.onMessage(requestReader.java:154)
2007-08-02 12:56:44,833 INFO STDOUT - at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2007-08-02 12:56:44,833 INFO STDOUT - at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
2007-08-02 12:56:44,833 INFO STDOUT - at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
2007-08-02 12:56:44,833 INFO STDOUT - at java.lang.reflect.Method.invoke(Method.java:324)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.invocation.Invocation.performCall(Invocation.java:345)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.MessageDrivenContainer$ContainerInterceptor.invoke(MessageDrivenContainer.java:475)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.plugins.MessageDrivenInstanceInterceptor.invoke(MessageDrivenInstanceInterceptor.java:87)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.plugins.CallValidationInterceptor.invoke(CallValidationInterceptor.java:48)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:105)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:313)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:146)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.plugins.RunAsSecurityInterceptor.invoke(RunAsSecurityInterceptor.java:94)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:192)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.MessageDrivenContainer.internalInvoke(MessageDrivenContainer.java:389)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.Container.invoke(Container.java:870)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.plugins.jms.JMSContainerInvoker.invoke(JMSContainerInvoker.java:967)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.ejb.plugins.jms.JMSContainerInvoker$MessageListenerImpl.onMessage(JMSContainerInvoker.java:1253)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.jms.asf.StdServerSession.onMessage(StdServerSession.java:256)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.mq.SpyMessageConsumer.sessionConsumerProcessMessage(SpyMessageConsumer.java:904)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.mq.SpyMessageConsumer.addMessage(SpyMessageConsumer.java:159)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.mq.SpySession.run(SpySession.java:351)
2007-08-02 12:56:44,833 INFO STDOUT - at org.jboss.jms.asf.StdServerSession.run(StdServerSession.java:180)
2007-08-02 12:56:44,833 INFO STDOUT - at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:743)
2007-08-02 12:56:44,833 INFO STDOUT - at java.lang.Thread.run(Thread.java:534)

Plz assist me

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jtobia
Offline
Joined: 2007-04-23

In get request the data is send encoded with the url.
In post request you must define the contend-length property and send your posted data before read from the url.

selvarevathi
Offline
Joined: 2007-01-29

hi thanx a lot for u r reply
is it correct way of defining the content length property
con.setRequestProperty("Content-type","text/html");
con.connect();
BufferedReader in = new BufferedReader(new InputStreamReadercon.getInputStream()));
response=in.readLine();
log.debug(classname + "Response Code from Platform-> "+response);
in.close();
con = null;

jtobia
Offline
Joined: 2007-04-23

Content-Length not Content-Type.

This is your code modified, i dont have tested it but i think it's work fine.

[code]
HttpURLConnection con = null;
URL urlnew = new URL (url);
con = (HttpURLConnection)urlnew.openConnection();
con.setRequestMethod("POST");
con.connect();

// Construct data
String data = new String();
data += URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode("meruodc", "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode("meru", "UTF-8");
data += "&" + URLEncoder.encode("message", "UTF-8") + "=" + URLEncoder.encode("test", "UTF-8");
data += "\r\n";

// Send data
con.setRequestProperty("Content-type","application/x-www-form-urlencoded");
con.setRequestProperty("Content-length",""+data.length());
con.setDoOutput(true);
con.setUseCaches(false);

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.close();

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
response=in.readLine();
in.close();

[/code]

Good luck
JTobia

selvarevathi
Offline
Joined: 2007-01-29

hi jtobia,

Thanx a lot 4 u r reply jtobia.
I have attached my code below.The Thing is im passing the URL as a String to this hitUrl(String url ) method.

The Url is
http://192.168.1.60:8080/gmt/hitWipAction?username=meruodc&password=meru....

This Url is passed as a Argument in this class.
URL urlnew = new URL (url);

public class urlHitter
{

public String hitUrl(String url) throws Exception
{
String response="";
try
{

HttpURLConnection con = null;
URL urlnew = new URL (url);
con = HttpURLConnectionurlnew.openConnection();
con.setRequestMethod("POST");
con.connect();

// Construct data
String data = new String();
data += URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode("meruodc", "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode("meru", "UTF-8");
data += "&" + URLEncoder.encode("message", "UTF-8") + "=" + URLEncoder.encode("test", "UTF-8");
data += "\r\n";

// Send data
con.setRequestProperty("Content-type","application/x-www-form-urlencoded");
con.setRequestProperty("Content-length",""+data.length());
con.setDoOutput(true);
con.setUseCaches(false);

OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(data);
wr.flush();
wr.close();

BufferedReader in = new BufferedReader(new InputStreamReadercon.getInputStream()));
response=in.readLine();
in.close();
}

}
plz assist me

jtobia
Offline
Joined: 2007-04-23

Your url is in GET Form. If your web service responde only in post method you must parse the url and traform it in post request (follow my previouse example for passing POST data), but if is possible modifiy the web service is more simple and correct trasform your web service for accept GET and POST http requeste