hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Vamsi Atluri <vkatl...@yahoo.com>
Subject Re: Post and https
Date Mon, 21 Apr 2003 18:48:01 GMT
Hi Iker,

Actually I have a problem (not so similar) with POST and HTTPS too. I am
trying to establish a connection to a https site and POST some data to it,
but the response is always a null. I am first doing a GET, which works
fine. But then the POST fails. The code I am using is:

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import java.io.*;
import java.security.*;
import java.util.*;

class TestSSLHttpClient
{
	public static void main(String[] args)
	{
		try
		{
			String url = "https://test.abc.com/xyz";
		
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
	  		System.setProperty("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");		
			System.out.println("Done setting SSL properties");
			
			HttpClient httpClient = new HttpClient();		
			System.out.println("Got HttpClient: " + httpClient);
		
			GetMethod getMethod = new GetMethod(url);
			System.out.println("Got HttpMethod: " + getMethod);
		
			int statusCode = -1;
		
			statusCode = httpClient.executeMethod(getMethod);
		
			System.out.println("statusCode: " + statusCode);
		
			byte[] responseBody = getMethod.getResponseBody();
			
			String resp = new String(responseBody);
		
			System.out.println("Response: " + resp);
			
			String xmlRequest = "<SomeXML>SomeXML</SomeXML>";
			
			PostMethod postMethod = new PostMethod(url);
			
			postMethod.setRequestBody(xmlRequest);
			
			System.out.println("Built PostMethod: " + postMethod);
			
			statusCode = -1;
			
			statusCode = httpClient.executeMethod(postMethod);
			
			System.out.println("statusCode: " + statusCode);
			
			resp = postMethod.getResponseBodyAsString();
			
			System.out.println("Response: " + resp);
			
			postMethod.releaseConnection();				
		}
		catch(Exception e)
		{
			System.out.println("#######################");
			System.out.println("Error occurred:");
			e.printStackTrace();
			System.out.println("#######################");
		}
	}
}

The error I get is that postMethod.getResponseBodyAsString() is returning
a null. However, if I try to trap the outpup of
postMethod.getResponseBody() into a byte[], I get the following exception:

Error occurred:
java.lang.NullPointerException
        at
org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMeth
odBase.java:670)
        at
org.apache.commons.httpclient.methods.GetMethod.getResponseBody(GetMe
thod.java:293)
        at TestSSLHttpClient.main(TestSSLHttpClient.java:54)

Any help in resolving this would be grately appreciated.

-Vamsi


--- Iker Bilbao <ibc19@tid.es> wrote:
> Hello,
> 
>   I am trying to make a post to an https server and I can not make it
>   work. I have what is was in the tutorial for posting but I get the
>   following error:
>             A recoverable exception occurred, retrying. 
> java.net.SocketException: Software caused connection
> abort: JVM_recv in socket input stream read
>   I have retried the operation but it does not work.
> 
>   The GetMehtod works well but not post.
> 
>   Here is my code:
>   -------------------
>   Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
>   System.getProperties().put("java.protocol.handler.pkgs",
> "org.apache.commons.httpclient.HttpClient");
>   System.setProperty("javax.net.ssl.trustStore","C:\\.keystore");
>   System.setProperty("javax.net.ssl.keyStore","C:\\.keystore");
>   System.setProperty("javax.net.ssl.keyStorePassword","password");
> 
>   HttpClient client = new HttpClient();
>   client.getState().setCredentials("Realm",new
> UsernamePasswordCredentials("user","password"));
> 
>   PostMethod post = new PostMethod("https://server:1234"
> +"/gestorpiloto");
>   NameValuePair[] data = {
>     new NameValuePair("tipoOperacion","alta")
>   };
> 
>   post.setRequestBody(data);
>   post.setDoAuthentication( true );
> 
>   try
>   {
>      int status = client.executeMethod( post );
>      System.out.println(status + "\n" + post.getResponseBodyAsString());
>   }
>   catch (HttpRecoverableException e)
>   {
>      System.err.println("A recoverable exception occurred, retrying.  "
> + e.getMessage());
>   }
>   catch (IOException e)
>   {
>      System.err.println("Failed to download file.");
>      e.printStackTrace();
>      System.exit(-1);
>   }
>   catch (Exception e)
>   {
>      e.printStackTrace();
>   }
>   
>   -------------------
> 
>   Has anyone any idea o what I am doing wrong?
> 
> Thanks in advance por your help.
> Iker
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> commons-httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> commons-httpclient-dev-help@jakarta.apache.org
> 


__________________________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo
http://search.yahoo.com

Mime
View raw message