1

大家好,我这里有个情况...

我正在开发一个 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();
    }
}

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

谢谢...

4

0 回答 0