java - 在java中检索cookieContainer内的数据

标签 java c# cookies unity-game-engine cookiecontainer

我正在使用 Unity 引擎开发一个游戏,它必须将 cookie 从客户端 C# 发送到服务器端 - Java,并且我面临这个问题(也许是跨平台问题?我不确定)

我在客户端写了一堆这样的代码

private HttpWebRequest request(){
    try{
        string url = "http://localhost:8080/...";
        var request = (HttpWebRequest)WebRequest.Create(url);
        request.Timeout = 15000;
        request.KeepAlive = true ;
        request.Method= "GET";

        CookieContainer cookieContainer = new CookieContainer();
        Cookie Authentication = new Cookie("Session" , "09iubasd");
        Authentication.Domain = url;
        cookieContainer.Add(Authentication);
        request.CookieContainer = cookieContainer;
        request.Headers.Add("testting", "hascome");
        return request;
    }catch(System.Exception ex){
        Debug.Log("[Exception]" + ex);
        throw ex;
    }

}

服务器端是用Java Spring编写的。我无法在服务器端检索 CookieContainer 内的 Cookie 数据。谁能给我任何建议或任何解决方案来解决这个问题?或者类似于Java中的CookieContainer的东西。我已经用谷歌搜索过,但似乎没有办法,如果这是一个愚蠢的问题,那么请教我。非常感谢。 文斯

最佳答案

我刚刚找出原因,我的 cookie 域设置错误。

这是我刚刚修复的新测试代码。希望这对将来遇到同样问题的人有所帮助(当然,如果没有人面对这个愚蠢的问题,那就太好了)

private HttpWebRequest request(){
    try{
        System.Uri uri = new System.Uri("http://localhost:8080/...");
        var request = (HttpWebRequest)WebRequest.Create(uri);
        request.Timeout = 15000;
        request.KeepAlive = true ;
        request.Method= "GET";

        Cookie Authentication = new Cookie("Session" , "09iubasd");
        Authentication.Domain = uri.Host;
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(Authentication);
        request.Headers.Add("testting", "hascome");
        return request;
    }catch(System.Exception ex){
        Debug.Log("[Exception]" + ex);
        throw ex;
    }

}

关于java - 在java中检索cookieContainer内的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37856148/

相关文章:

java - 另一个 rJava 库安装错误 : rJava. rdb' is corrupt

c# - 如何从 aspx 中的 Gridview 行检索主键以在 userControl.ascx 中使用?

c# - 使用 XmlMessageFormater 序列化的消息拒绝反序列化 List<T> 成员

Angular 8 - 使用 CookieService 清除 cookie

Java 摘要哈希和 PHP 哈希不同

java - 如何获取本地化的日期模式字符串?

javascript - 仅当 cookie 存在时才添加Class,否则隐藏

PHP cookie 转化为字符串

Java 控制台应用程序打开并运行 JAR 文件

c# - 为什么C#有static Main()而C++没有static main