如何在URLConnection中发送cookie? [英] How to send a cookie in a URLConnection?

查看:155
本文介绍了如何在URLConnection中发送cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在URLConnection上发送'完整'cookie的正确方法是什么?

What is the proper way to send a 'full' cookie across a URLConnection?

我一直在使用:

URL url = new URL(page);  
URLConnection urlConn = url.openConnection(); 

urlConn.setRequestProperty("Cookie", myCookie); 

urlConn.setUseCaches(true); 

urlConn.connect();

myCookie值为
testCookie = d1lEZk9rSHd3WnpBd2JkWGRhN1RYdz09OkEwQ21pSFJVZzBpVDhhUENaK3ZPV2c9PQ

The myCookie value is testCookie=d1lEZk9rSHd3WnpBd2JkWGRhN1RYdz09OkEwQ21pSFJVZzBpVDhhUENaK3ZPV2c9PQ

有没有办法用它发送Path,Domain和Expires?你需要以某种方式对值进行编码吗?

Is there a way to send the Path,Domain, and Expires with it? Do you need to encode the value in some way?

推荐答案

好吧,如果你只是设置一个cookie,我猜你可以简单一点喜欢:

Well, if you are only setting a cookie I guess you could simply do like:

urlConn.setRequestProperty("Cookie", "user=mary17; domain=airtravelbargains.com; path=/autos");

如果你设置多个cookie,你可能会使用相反,addRequestProperty 方法。

If you're setting more than one cookie than you could probably use the addRequestProperty method instead.

对于expires属性,请确保使用以下格式:Weekday,DD-Mon-YY HH:MM:SS GMT 。

For the expires attribute make sure to use the format: Weekday, DD-Mon-YY HH:MM:SS GMT.

唯一的合法时区是GMT,日期元素之间的分隔符必须是
破折号。

The only legal time zone is GMT, and the separators between the elements of the date must be dashes.

这篇关于如何在URLConnection中发送cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆