You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Pan John-CXP001 <jo...@motorola.com> on 2005/07/20 05:42:01 UTC

Help on the httpclient

I wrote a simple http client to talk with the http server running on one
of our routers. The server version is 1.0.  However, when I do a GET on
the router, I got the following error message.
 
2005-7-19 20:35:10
org.apache.commons.httpclient.auth.AuthChallengeProcessor
selectAuthScheme
info: basic authentication scheme selected
Fatal protocol violation: The server 10.79.134.111 failed to respond
with a valid HTTP response
org.apache.commons.httpclient.ProtocolException: The server
10.79.134.111 failed to respond with a valid HTTP response
 at
org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBa
se.java:1846)
 at
org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase
.java:1592)
 at
org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java
:995)
 at
org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMe
thodDirector.java:393)
 at
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMetho
dDirector.java:168)
 at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:3
96)
 at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:3
24)
 at com.motorola.moca.bcm.bcmutils.BcmRuncmd.main(BcmRuncmd.java:57)

When I use the IE browser to access the same webpage
"http://10.79.134.111/execcmd?sh&vers
<http://10.79.134.111/execcmd?sh&vers> &" on my router, it works fine.
I am confused since I don't know it's my client problem or it's router's
http server problem.
 
Any help will be appreciated;
 
 
--John
 
 
------------------------------------------------------------------------
-------------------------------
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.auth.*;
import org.apache.commons.httpclient.HttpVersion;
import java.io.*;
 
public class BcmRuncmd {
  public BcmRuncmd() {
  }

  // Define the url
  // http://10.79.134.111/execcmd?sh&vers
<http://10.79.134.111/execcmd?sh&vers> &  -- works on IE
  private static String url = "http://10.79.134.111/execcmd?sh&vers
<http://10.79.134.111/execcmd?sh&vers> &";
  //private static   String url =
"http://mail-archives.apache.org/mod_mbox/jakarta-httpclient-user/200507
.mbox/threads.html
<http://mail-archives.apache.org/mod_mbox/jakarta-httpclient-user/200507
.mbox/threads.html> ";

    public static void main(String[] args) {
     // Create an instance of HttpClient.
      HttpClient client = new HttpClient();
 
      //Set Authentication
      client.getParams().setAuthenticationPreemptive(true);
      client.getParams().setVersion(HttpVersion.HTTP_1_0);
      UsernamePasswordCredentials upc = new
UsernamePasswordCredentials("john", "mypwd");
      client.getState().setCredentials(AuthScope.ANY,upc);
 
      // Create a method instance.
      GetMethod method = new GetMethod(url);
      method.setDoAuthentication(true);
 
      // Provide custom retry handler is necessary
      method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
 
      try {
        // Execute the method.
        int statusCode = client.executeMethod(method);
 
        System.out.println("Status Text >>> " +
HttpStatus.getStatusText(statusCode));
        if (statusCode != HttpStatus.SC_OK) {
          System.err.println("Method failed: " +
method.getStatusLine());
        }
 

        // Read the response body.
         System.out.println(new
String(method.getResponseBodyAsString()));
 
      } catch (HttpException e) {
        System.err.println("Fatal protocol violation: " +
e.getMessage());
        e.printStackTrace();
      } catch (IOException e) {
        System.err.println("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
      } finally {
        // Release the connection.
        method.releaseConnection();
      }
    }
}