I am sending request in the following way.
HttpClient httpClient=new DefaultHttpClient();
CookieStore cookieStore=new BasicCookieStore();
HttpContext httpContext=new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
httpClient.execute(httppost,httpContext);
My auth cookie on java side reads like this.
XNlciI6WzU2Mjk0OTk1MzQyMTMxMjAsMCwidEFlbVlLYlpuRXYyc29TNjBSOHhueCIsMTM3MzM0NzcyMCwxMzczMzQ3NzIwXX0\075|1373347720|5c1ad3ac3828516aa7178f00b3bba961fa29ae9b
(notice \075)
On server side it is like this.
XNlciI6WzU2Mjk0OTk1MzQyMTMxMjAsMCwiejJYUXpQQVhBQ0lQVkdCQU5FMkRtdSIsMTM3MzM0OTIyNiwxMzczMzQ5MjI2XX0
When I use python requests the cookie shown is as follows.
XNlciI6WzU2Mjk0OTk1MzQyMTMxMjAsMCwiclYzYW1FakRHc0dhampDcnhoMlBIVyIsMTM3MzM0OTEzNiwxMzczMzQ5MTM2XX0=|1373349137|e8900c8bfd2972ca4115ef1946b4cdf161a4815a
It appears that HttpClient is ignoring the bit after | (date code and stuff). Am I missing something? I tried all the cookie policies as well, nothing working.
OK I solved it in following way. Before I answer a few facts.
This is my entire post method.
public String doUrlPost(final String connurl,final JSONObject obj) throws IOException{
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
URL url=new URL(connurl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//sets the cookie to version 1
urlConnection.setRequestProperty("Cookie2","$Version=1");
List<HttpCookie> lst=((CookieManager)CookieHandler.getDefault()).getCookieStore().getCookies();
for(HttpCookie cookie:lst){
if(cookie.getName().equals("auth")){
//double quote your cookie
urlConnection.setRequestProperty("Cookie","auth=\""+cookie.getValue()+"\"");
}
}
urlConnection.setUseCaches(false);
List<NameValuePair> nameValuePairs = getData(obj);
OutputStream out = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(out, "UTF-8"));
writer.write(getQuery(nameValuePairs));
writer.close();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader rd=new BufferedReader(new InputStreamReader(in));
String line="";
String content="";
while((line=rd.readLine())!=null){
content+=line;
}
rd.close();
finalcontent=content;
urlConnection.disconnect();
return finalcontent;
}
Get query method
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With