Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Http Client Component  RSS feed

 
surya thandra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What is the best Http Client component for a java application to communicate with a remote web server .Basically we are trying to submit info to a webportal server programatically and get the response back from that remote server.
Thanks in advance
Surya
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the java.net.URL class doesn't do what you want, try the Apache Jakarta HttpClient.
- Peter
 
surya thandra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
I tried using the HTTP Clinet Component from Apache Iam getting the following errors.
My Code:
org.apache.commons.httpclient.Header CONTENT_TYPE = new org.apache.commons.httpclient.Header("Content-Type","text/html");
org.apache.commons.httpclient.HttpClient client=new org.apache.commons.httpclient.HttpClient();
//org.apache.commons.httpclient.HttpConnection con=new org.apache.commons.httpclient.HttpConnection("portal.cpn.vwg",);
//org.apache.commons.httpclient.HttpState httpState= new org.apache.commons.httpclient.HttpState();
org.apache.commons.httpclient.methods.PostMethod post = new org.apache.commons.httpclient.methods.PostMethod();
post.setFollowRedirects(true);
java.net.URL url=new java.net.URL("http://xyz.abc.com/");
org.apache.commons.httpclient.NameValuePair form_data[] = new org.apache.commons.httpclient.NameValuePair[4];

form_data[0] = new org.apache.commons.httpclient.NameValuePair("language", "2");
form_data[1] = new org.apache.commons.httpclient.NameValuePair("user","amndsmndjn");
form_data[2] = new org.apache.commons.httpclient.NameValuePair("dealer","jnjfbvjf");
form_data[3] = new org.apache.commons.httpclient.NameValuePair("password","vfjbvfS");




post.setQueryString("/verifyuser.asp?language=");
post.setQueryString(form_data);

client.startSession(url);
client.executeMethod(post);
org.apache.commons.httpclient.Header resp[] =post.getResponseHeaders();

for(int i=0;i<resp.length;i++){
System.out.println(resp[i]);

}
final String strGetResponseBody =post.getResponseBodyAsString();
System.out.println(strGetResponseBody);
ERROR MESSAGES:
Server: Microsoft-IIS/4.0
HTTP/1.1 405 Method not allowed
Server: Microsoft-IIS/4.0
Date: Fri, 06 Dec 2002 22:58:06 GMT
Connection: close
Allow: OPTIONS, TRACE, GET, HEAD
Content-Length: 545
Content-Type: text/html
<html><head><title>Error 405</title>
<meta name="robots" content="noindex">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"></head>
<body>
<h2>HTTP Error 405</h2>
<p><strong>405 Method Not Allowed</strong></p>
<p>The method specified in the Request Line is not allowed for the resource identified by the request. Please ensure that you have the proper MIME type set up for the resource you are requesting.</p>
<p>Please contact the server's administrator if this problem persists.</p>
</body></html>
I was able to do this succesfully with other HTTP clinets ,but we have to go with Apache.
Any Suggestions Appreciated.
Thanks
Thandra
 
Michael Zalewski
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use GetMethod instead of PostMethod.
Also, this code

doesn't look right. You can set the querystring as either a String or an array NameValuePair[], but not both. The ?language= is part of the querystring. Try
 
surya thandra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike,
I have correcedted that Query String.I got a Error Code 400 Bad syntax in request.
<br>
Here i need to post some form data and get the response back.I was able to achieve this by Post using Innovation API but we have it do it by apache.
<p>
Suggestions appreciated.
Regards,
Thandra
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!