hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pvbem...@xs4all.nl
Subject Re: httpcore parses expires in Set-Cookie wrong
Date Mon, 12 Nov 2012 19:57:50 GMT

Hi Oleg,

>
> Given it is a very common protocol violation HttpClient ships with a
> number of cookie policies (BROWSER_COMPATIBILITY, NETSCAPE_DRAFT as well
> as BEST_MATCH) that are capable of parsing such headers. There is
> nothing special one needs to do. Simply let HttpClient's default cookie
> policy do its job.
>
> Oleg
>

Then why does my program throw exceptions like

  java.text.ParseException: Unparseable date: "Tue"

when it comes to parsing date strings in Set-Cookie headers ?


I posted the program earlier; it's repeated below.
//------------------------------------------------------------------------

package nl.xs4all.pvbemmel.livejasmin;
import java.io.*;
import java.text.*;
import java.text.ParseException;
import java.util.*;

import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.impl.cookie.*;
public class InvalidSetCookieProblem {
  public static void main(String[] args) throws
      ClientProtocolException, IOException {

    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(
"http://www.livejasmin.com/listpage.php?tags=girl&type=40&openQuicklist");
    HttpResponse response = httpClient.execute(httpGet);
    System.out.println(response.getStatusLine());
    Header[] headers = response.getHeaders("Set-Cookie");
    processHeaders0(headers);
    processHeaders1(headers);
    httpClient.getConnectionManager().shutdown();
  }
  private static void processHeaders0(Header[] headers) {
    for(Header header : headers) {
      System.out.println(""+header);
    }
  }
  /**
   * {@linkplain HeaderElement}
   * @param headers
   */
  private static void processHeaders1(Header[] headers) {
    for (Header header : headers) {
      System.out.println("Header: " + header.getName() + " : "
          + header.getValue());
      String hn = header.getName();
      String hv = header.getValue();
      BasicClientCookie2 bcc2 = new BasicClientCookie2(hn, hv);

      HeaderElement[] elements = header.getElements();
      for (HeaderElement elem : elements) {

        NameValuePair[] pairs = elem.getParameters();
        for (NameValuePair pair : pairs) {
          System.out.println("NameValuePair: " + pair.getName() + " : "
              + pair.getValue());
          String pn = pair.getName();
          String pv = pair.getValue();

          if (pn.equals("expires")) {
            SimpleDateFormat sdf = new SimpleDateFormat(
                "EEE, dd-MMM-yyy HH:mm:ss zzz",
                DateFormatSymbols.getInstance(new Locale("en")));
            Date d = null;
            try {
              d = sdf.parse(pv);
            }
            catch (ParseException e) {
              System.out.println(e);
            }
            bcc2.setExpiryDate(d);
          }
        }
      }
    }
  }
}
//------------------------------------------------------------------------

Paul.



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


Mime
View raw message