views:

140

answers:

1

I want to upload a txt file to a website, I'll admit I haven't looked into it in any great detail but I have looked at a few examples and would like more experienced opinions on whether I'm going in the right direction.

Here is what I have so far:


DefaultHttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
private String ret;

HttpResponse response = null;
HttpPost httpPost = null;


public String postPage(String url, String data, boolean returnAddr) {

    ret = null;

    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);

    httpPost = new HttpPost(url);
    response = null;

    StringEntity tmp = null;         

    try {
        tmp = new StringEntity(data,"UTF-8");
    } catch (UnsupportedEncodingException e) {
        System.out.println("HTTPHelp : UnsupportedEncodingException : "+e);
    }

    httpPost.setEntity(tmp);

    try {
        response = httpClient.execute(httpPost,localContext);
    } catch (ClientProtocolException e) {
        System.out.println("HTTPHelp : ClientProtocolException : "+e);
    } catch (IOException e) {
        System.out.println("HTTPHelp : IOException : "+e);
    } 
            ret = response.getStatusLine().toString();

            return ret;
}

And I call it as follows:


postPage("http://www.testwebsite.com", "data/data/com.testxmlpost.xml/files/logging.txt", true));

I want to be able to upload a file from the device to a website.

But when trying this way I get the following response back.


HTTP/1.1 405 Method Not Allowed

Am I trying the correct way or should I be doing it another way?

+2  A: 

That code looks reasonable, the error is from the server and indicates that POST is not allowed for that page.

You're sending the literal string "data/data/com.testxmlpost.xml/files/logging.txt". If you want to post a file, use a FileEntity.

Matthew Flaschen
Thanks I was thinking that, Can I ask though is the code trying to post the file located at data/data/com.testxmlpost.xml/files/logging.txt or is it trying to post the String "data/data/com.testxmlpost.xml/files/logging.txt" ?
Donal Rafferty
The string. You can use a FileEntity instead.
Matthew Flaschen
Thanks Matthew, I have changed it to a file entity and I am using a server that has been set up to allow uploading but I am getting the same response, is there a different way to do it other than using POST?
Donal Rafferty
@Donal, which entity you use and which method are separate. As long as you use HttpPost, it will be a POST request. You need to find out exactly what method and format the server expects you to use.
Matthew Flaschen
One more question Matthew, I have found out that the server expects the PUT method and have changed to that. The response I get is 403 Forbidden, from reading up on this am I correct in saying it is not a authorization error and it instead means that the Server recognises the method but doesn't like the format? And by format that is how I set up my HttpClient and HttpPUT?
Donal Rafferty
The server could be giving that error for multiple reasons. It could expect [basic authentication](http://en.wikipedia.org/wiki/Basic_access_authentication), but it could also be something else.
Matthew Flaschen