title: Get Cookies & Session and set again using HttpClient
body: <p>Hey guys i have a situation here...</p>
  
  <p>Im working on a java program where my client want to login to a website with the given credentials and then upload a file to an url of the same website... The problem is either the client done know whether the user login is authenticated and set using cookie or session..</p>
  
  <p>Im using HttpClient for both login and upload and i finished writing the code.. The problem is when upload a file i get "403 Forbidden". Is there a way to get the list of cookies and sessions so that i can fetch it from login url output and set it to upload as to think that the request is from an authenticated user.</p>
  
  <p>For more reference im attaching the script:</p>
  
  <pre><code>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&lt;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();
      }
  }
  </code></pre>
  
  <p>I need to show the upload page that the user is logged in so that it can upload...</p>
  
  <p>Thank you...</p>