You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Rezvani, Matt" <MR...@orionsci.com> on 2003/09/23 17:26:09 UTC

A retry on a PutMethod

When setting the request content length of an enclosed entity to be stored
on the WebDAV server using the PutMethod, the HttpClient returns an
"Unbuffered entity enclosing request can not be repeated" HttpException. I
am aware that if the request is not buffered, then the PutMethod (or
PostMethod) cannot retry the request in case of an authentication challenge
or an I/O error. An alternate solution would be to let the PutMethod buffer
the entire file by setting the request content length to
EntityEnclosingMethod.CONTENT_LENGTH_AUTO. This is OK for small files;
however for enormous size files it causes an out-of-memory error. My
question is how do I send a retry request after an authentication challenge
has been sent back by the server?

Here is a sample code.

//--------------------------
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.PutMethod;
import java.io.*;

public class PutMethodExample
{
		public static void main(String args[]) throws Exception
	{
		HttpClient client = new HttpClient();
		// Assuming that the credentials are passed to the
HttpClient using
		// the HttpState.setCredentials

		PutMethod put = new
	
PutMethod("http://myServer:/webdav/example.txt");
		File bigFile = new File("c:\\j2sdk-1_4_2-windows-i586.exe");
		long fileLength = bigFile.length();
		put.setRequestContentLength((int)fileLength);
		put.setRequestBody(new FileInputStream(bigFile));

		int status = client.executeMethod(put);
		put.releaseConnection();
	}
}

Thanks,
Matt