gpt4 book ai didi

java - 在 Java 中处理包含等于 ("=") 的 Cookie

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:47 24 4
gpt4 key购买 nike

我正在通过以下方式发送请求。

    HttpClient httpClient=new DefaultHttpClient();
CookieStore cookieStore=new BasicCookieStore();
HttpContext httpContext=new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
httpClient.execute(httppost,httpContext);

我在 java 端的 auth cookie 是这样写的。

XNlciI6WzU2Mjk0OTk1MzQyMTMxMjAsMCwidEFlbVlLYlpuRXYyc29TNjBSOHhueCIsMTM3MzM0NzcyMCwxMzczMzQ3NzIwXX0\075|1373347720|5c1ad3ac3828516aa7178f01b29bae

(注意\075)

服务器端是这样的

XNlciI6WzU2Mjk0OTk1MzQyMTMxMjAsMCwiejJYUXpQQVhBQ0lQVkdCQU5FMkRtdSIsMTM3MzM0OTIyNiwxMzczMzQ5MjI2XX0

当我使用 python 请求时,显示的 cookie 如下。

XNlciI6WzU2Mjk0OTk1MzQyMTMxMjAsMCwiclYzYW1FakRHc0dhampDcnhoMlBIVyIsMTM3MzM0OTEzNiwxMzczMzQ5MTM2XX0=|1373349137|e8900c8bfd2972ca4115ef1946b14cdf15

HttpClient 似乎忽略了 | 之后的位(日期代码和东西)。我错过了什么吗?我也尝试了所有 cookie 策略,但没有任何效果。

最佳答案

好的,我通过以下方式解决了它。在我回答一些事实之前。

  1. Cookie 键值对由“=”分隔。
  2. 必须对其中包含“=”的 Cookie 进行转义(“=”部分)。
  3. 包含“=”的cookie需要指定为Version1 cookie
  4. 此类 cookie 必须用双引号括起来。

这是我的整个发布方法。

    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;
}

获取查询方法

    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();
}

关于java - 在 Java 中处理包含等于 ("=") 的 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17541385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com