flutter - 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));
}
解决方案
问题解决了。
实际上,服务器需要会话 cookie(MYSAPSSO 和 SAP_SESSIONID)以及 x-csrf-token。
推荐阅读
- sql - 查询数据库中更新的记录
- c - 如何在c中将字符串存储在数组中?
- flexlm - 将 MAX 用于具有 Flexlm 许可的 OPTIONS 文件中的所有功能
- python - cx_freeze Winerror 2 系统找不到指定的文件
- algorithm - 3 与特定重叠的组合
- django - Django:发布表单数据时找不到页面(404)
- qt - QMutex 被未知线程锁定
- python - 为什么 sys.stdout.write 输出意外字符?
- javascript - Javascript Pretty Print Format 给出错误的结果
- javascript - React Modal 提前关闭