首页 > 解决方案 > Http Post Flutter 到 SAP

问题描述

我试图使用 http post 将数据从颤振传输到 SAP。我可以毫无问题地获取数据,但发布尝试失败,代码为 403(x-csrf-token 无效)

我在 C# 中工作时遇到了同样的问题,但是使用事件处理程序解决了这个问题,它在保存之前触发(请参阅下面的 C# 代码摘录),但我无法在颤振中找到选项。请指导..

   zZSSALE1SRVEntity.SendingRequest2 += new EventHandler<SendingRequest2EventArgs>(_container_SendingRequest_Enhance);
   zZSSALE1SRVEntity.SaveChanges();

   private void _container_SendingRequest_Enhance(object sender, SendingRequest2EventArgs e)
    {
        HttpWebResponse response;
        string empty = string.Empty;
        string str = string.Empty;
        CookieContainer cookieContainer = new CookieContainer();
        OdataSsaleDEV.ZZSSALE1_SRV_Entities zZSSALE1SRVEntity = new OdataSsaleDEV.ZZSSALE1_SRV_Entities(app_uri)
        {
            Credentials = credentials
        };
        string str1 ;
        if (empty == string.Empty)
        {
            HttpWebRequest credentials = (HttpWebRequest)WebRequest.Create(zZSSALE1SRVEntity.BaseUri);
            credentials.Method = "GET";
            credentials.Headers.Add("X-CSRF-Token", "Fetch");
            credentials.Credentials = zZSSALE1SRVEntity.Credentials;
            cookieContainer = new CookieContainer();
            credentials.CookieContainer = cookieContainer;
            try
            {
                response = (HttpWebResponse)credentials.GetResponse();
            }
            catch (WebException webException)
            {
                MessageBox.Show(webException.Message);
                return;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                return;
            }
            empty = response.Headers.Get("X-CSRF-Token");
            str = response.Headers.Get("Set-Cookie");
            credentials.Abort();
        }
        if (empty != string.Empty)
        {
            e.RequestMessage.SetHeader("x-csrf-token", empty);
            foreach (Cookie cooky in cookieContainer.GetCookies(zZSSALE1SRVEntity.BaseUri))
            {
                str1 = string.Concat(str1, ";", cooky.ToString());
            }
            e.RequestMessage.SetHeader("Cookie", str1.Substring(1));
        }

标签: flutter

解决方案


问题解决了。

实际上,服务器需要会话 cookie(MYSAPSSO 和 SAP_SESSIONID)以及 x-csrf-token。


推荐阅读