获取Cookies&会话并使用HttpClient再次设置

时间:2012-05-21 11:20:11

标签: java swing file-upload httpclient

嘿伙计们,我有一个情况......

我正在开发一个java程序,我的客户端想要登录到具有给定凭据的网站,然后将文件上传到同一网站的URL ...问题是客户端是否知道用户登录是否是使用cookie或会话进行身份验证和设置..

我使用HttpClient进行登录和上传,我完成了代码编写..问题是当上传文件时我得到“403 Forbidden”。有没有办法获取cookie和会话列表,以便我可以从登录URL输出中获取它并将其设置为上传,以便认为该请求来自经过身份验证的用户。

有关更多参考,请附上脚本:

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.apache.http.Header;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;

public class Main {

    Header[] cookie = null;

    public void sendLoginRequest() {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

            HttpPost httppost = new HttpPost("http://website.com/login");

            MultipartEntity mpEntity = new MultipartEntity();
            mpEntity.addPart("email", new StringBody("user@website.com"));
            mpEntity.addPart("password", new StringBody("123456"));

            httppost.setEntity(mpEntity);
            System.out.println("executing request " + httppost.getRequestLine());
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();

            System.out.println(response.getStatusLine());
            if (resEntity != null) {
                System.out.println(EntityUtils.toString(resEntity));
            }
            if (resEntity != null) {
                resEntity.consumeContent();
            }

            cookie = (Header[]) response.getHeaders("Set-Cookie");
            for(int h=0; h<cookie.length; h++){
                System.out.println(cookie[h]);
            }

            httpclient.getConnectionManager().shutdown();

            sendFileRequest();
        } catch (IOException ex) {
            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void sendFileRequest(){
        try {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

            HttpPost httppost = new HttpPost("http://website.com/uploadpage");
            httppost.setHeaders(cookie);

            File file = new File("somefile.jpg");

            MultipartEntity mpEntity = new MultipartEntity();
            ContentBody cbFile = new FileBody(file, "image/jpeg");
            mpEntity.addPart("Filedata", cbFile);
            mpEntity.addPart("MAX_FILE_SIZE", new StringBody("268435456"));

            httppost.setEntity(mpEntity);
            System.out.println("executing request " + httppost.getRequestLine());
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();

            System.out.println(response.getStatusLine());
            if (resEntity != null) {
                System.out.println(EntityUtils.toString(resEntity));
            }
            if (resEntity != null) {
                resEntity.consumeContent();
            }

            httpclient.getConnectionManager().shutdown();
        } catch (IOException ex) {
            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) {
        new Main().sendLoginRequest();
    }
}

我需要显示用户登录的上传页面,以便上传...

谢谢...

0 个答案:

没有答案