Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用生成的令牌访问第二个服务_C#_Web Services_Rest_Soap_Drupal Services - Fatal编程技术网

C# 无法使用生成的令牌访问第二个服务

C# 无法使用生成的令牌访问第二个服务,c#,web-services,rest,soap,drupal-services,C#,Web Services,Rest,Soap,Drupal Services,我已经获得了两个服务,其中ist服务用于登录,它将返回一个xml节点令牌,我将在所有其他服务调用中添加该令牌作为头 在所附的示例代码中,函数CreateObject中有两个函数,我正在点击登录服务并成功获得响应,从响应中我获取了令牌,并使用令牌searchstring令牌调用了其他函数,并在标头中传递了令牌,但它给出了一个异常[System.Net.WebException]={远程服务器返回错误:403禁止。} 但当我在chrome高级Rest客户端中看到它给出了预期的响应 任何方向正确的建议

我已经获得了两个服务,其中ist服务用于登录,它将返回一个xml节点令牌,我将在所有其他服务调用中添加该令牌作为头

在所附的示例代码中,函数CreateObject中有两个函数,我正在点击登录服务并成功获得响应,从响应中我获取了令牌,并使用令牌searchstring令牌调用了其他函数,并在标头中传递了令牌,但它给出了一个异常[System.Net.WebException]={远程服务器返回错误:403禁止。}

但当我在chrome高级Rest客户端中看到它给出了预期的响应

任何方向正确的建议或解决方案都是非常值得赞赏的

请参阅下面的代码

 private  void CreateObject()
    {        
        try
        {
            string abc = "";
            Stream datastream;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("www.example.com/abc/login");
            request.CookieContainer = new CookieContainer();
            request.AllowAutoRedirect = true;
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "application/xml";
            request.KeepAlive = true;
            String postData = "";
            request.Method = "POST";
            postData = String.Format("username={0}&password={1}", "sample", "sample");
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = byteArray.Length;
            datastream = request.GetRequestStream();
            datastream.Write(byteArray, 0, byteArray.Length);


            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            datastream = response.GetResponseStream();
            String sourceCode = "";
            using (StreamReader reader = new StreamReader(datastream))
            {
                sourceCode = reader.ReadToEnd();
            }
            int pos = sourceCode.IndexOf("<?");
            string xmlpart = sourceCode.Substring(pos);
            //XmlReader reader1 = XmlReader.Create(sourceCode);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlpart);
            XmlNodeList objNodeList = doc.GetElementsByTagName("token");
           string tokens = objNodeList[0].ChildNodes.Item(0).InnerText.Trim();
          search(tokens);

        }
        catch (Exception e)
        {

        }
    }

     public void search(string token)
        {
            Stream datastream;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://example.com/services/suggestions/search");
            request.CookieContainer = new CookieContainer();
           // request.AllowAutoRedirect = true;
            System.Net.ServicePointManager.Expect100Continue = false;
           request.Headers.Add("x-csrf-token", token);
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "application/x-cdf,text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8";
            String postData = "";
            request.Method = "POST";
            request.KeepAlive = true;
            postData = String.Format("param1={0}&param2={1}&param3={2}&param4={3}&param5={4}&param6={5}&param7={6}&param8={7}", "0", "1","1","1","0","1","2015-06-22","2015-06-22");
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = byteArray.Length;
            datastream = request.GetRequestStream();
            datastream.Write(byteArray, 0, byteArray.Length);


            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            datastream = response.GetResponseStream();
            String sourceCode = "";
            using (StreamReader reader = new StreamReader(datastream))
            {
                sourceCode = reader.ReadToEnd();
            }

        }

最后我发现了问题所在

我正在为这两个请求创建新的cookie容器

request.CookieContainer = new CookieContainer();
因此,服务器无法进行身份验证

错误已通过使用此代码解决

CookieContainer cookieJar = new CookieContainer();
    private  void CreateObject()
        {        
            try
            {
                string abc = "";
                Stream datastream;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("www.example.com/abc/login");
                request.CookieContainer = cookieJar;
                request.AllowAutoRedirect = true;
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "application/xml";
                request.KeepAlive = true;
                String postData = "";
                request.Method = "POST";
                postData = String.Format("username={0}&password={1}", "sample", "sample");
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentLength = byteArray.Length;
                datastream = request.GetRequestStream();
                datastream.Write(byteArray, 0, byteArray.Length);


                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                datastream = response.GetResponseStream();
                String sourceCode = "";
                using (StreamReader reader = new StreamReader(datastream))
                {
                    sourceCode = reader.ReadToEnd();
                }
                int pos = sourceCode.IndexOf("<?");
                string xmlpart = sourceCode.Substring(pos);
                //XmlReader reader1 = XmlReader.Create(sourceCode);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlpart);
                XmlNodeList objNodeList = doc.GetElementsByTagName("token");
               string tokens = objNodeList[0].ChildNodes.Item(0).InnerText.Trim();
              search(tokens);

            }
            catch (Exception e)
            {

            }
        }

         public void search(string token)
            {
                Stream datastream;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://example.com/services/suggestions/search");
                request.CookieContainer = cookieJar; // same cookejar is used as was used in login call
               // request.AllowAutoRedirect = true;
                System.Net.ServicePointManager.Expect100Continue = false;
               request.Headers.Add("x-csrf-token", token);
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "application/x-cdf,text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8";
                String postData = "";
                request.Method = "POST";
                request.KeepAlive = true;
                postData = String.Format("param1={0}&param2={1}&param3={2}&param4={3}&param5={4}&param6={5}&param7={6}&param8={7}", "0", "1","1","1","0","1","2015-06-22","2015-06-22");
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentLength = byteArray.Length;
                datastream = request.GetRequestStream();
                datastream.Write(byteArray, 0, byteArray.Length);


                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                datastream = response.GetResponseStream();
                String sourceCode = "";
                using (StreamReader reader = new StreamReader(datastream))
                {
                    sourceCode = reader.ReadToEnd();
                }

            }