You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by STEINER Stephan <St...@nextiraone.ch> on 2006/03/29 19:33:54 UTC

Trying to create a SearchMethod

Hi

When I was all done and ready to test my WebDAV search request using
basic Java HttpURLConnection, I had to find out the bugger doesn't
support HTTP SEARCH. Oh well, I started looking around, found a 2001
client that's supposed to work but that I couldn't even compile without
pages full of errors. Then I had a look at slide but it wouldn't do the
trick either (no examples..), so I figured it can't be that hard and
since I already have everything else, let's write an additional HTTP
Method for the httpclient.

Unfortunately, Exchange consistantly balked at me so I started Ethereal
and found out that my SEARCH request is in fact a POST with Method
SEARCH. Here's how the first few lines form a proper request (C# has no
problem with SEARCH requests... Is Sun listening?)

SEARCH /exchange/stephan.steiner@nextiraone.ch/Kontakte  HTTP/1.1
Authorization: Basic
Content-Type: text/xml. 

My own request, which extends EntityEnclosingMethod (I figured I needed
to start with a request that involves sending data back to the webserver
since a WEBDAV Search is just that.. Send XML to the server, and get an
XML response back). Ethereal shows the following for my Java search
class:

POST /exchange/stephan.steiner@nextiraone.ch/Kontakte HTTP/1.1. 
Method: SEARCH
Content- Length: 552
Content-Type: text/ xml
Authorization: Basic

So that's not going to work. Now I've been looking through a lot of code
but for the life of me can't find where POST is sent over the line so
I'd appreciate your input.

For your reference I'm pasting my class at the end of this message.

Regards
Stephan



package phonenumberlookup;

import java.net.URLEncoder;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.util.EncodingUtil;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
/**
 *
 * @author SSTE
 */
public class SearchMethod extends
org.apache.commons.httpclient.methods.EntityEnclosingMethod
{
    private String searchString;
    /** Creates a new instance of SearchMethod */
    public SearchMethod()
    {
        super();
        this.setRequestHeader("Content-Type", "text/xml");
    }
    
    public SearchMethod(String uri)
    {
        super(uri);
        this.setRequestHeader("Content-Type", "text/xml");
    }
    
    public SearchMethod(String uri, String query)
    {
        this(uri);
        this.setSearchString(query);
    }
    
    public String getName()
    {
        return "SEARCH";
    }
    
    protected boolean shouldCloseConnection(HttpConnection conn)
    {
        return true;
    }
    
    protected RequestEntity generateRequestEntity() 
    {
        if (this.searchString != null)
        {            
            try
            {
                String content = URLEncoder.encode(this.searchString,
"UTF-8");
            }
            catch (java.io.UnsupportedEncodingException u)
            {
                return super.generateRequestEntity();
            }
            ByteArrayRequestEntity entity = new
ByteArrayRequestEntity(EncodingUtil.getAsciiBytes(this.searchString),
"text/xml");
            return entity;
        } 
        else 
        {
            return super.generateRequestEntity();
        }
    }

    public String getSearchString()
    {
        return searchString;
    }

    public void setSearchString(String searchString)
    {
        this.searchString = searchString;
    }
    
}

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Trying to create a SearchMethod

Posted by Roland Weber <RO...@de.ibm.com>.
Hello Stephan,

> Unfortunately, Exchange consistantly balked at me so I started Ethereal
> and found out that my SEARCH request is in fact a POST with Method
> SEARCH. Here's how the first few lines form a proper request (C# has no
> problem with SEARCH requests... Is Sun listening?)
> 
> SEARCH /exchange/stephan.steiner@nextiraone.ch/Kontakte  HTTP/1.1
> Authorization: Basic
> Content-Type: text/xml. 

You can extend PostMethod and override getName() to change the method 
name:
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/HttpMethodBase.html#getName()

The above is not valid HTTP/1.1, since there is no Host: header.

hope that helps,
  Roland

 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org