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: [email protected]
For additional commands, e-mail: [email protected]