Flutter Http Post颤振到SAP
我正在尝试使用HTTPPOST将数据从Flatter传输到SAP。我可以毫无问题地获取数据,但post尝试失败,代码403(x-csrf-token无效) 我在使用C#时也遇到了同样的问题,但这是通过使用事件处理程序解决的,它在保存之前触发(请参见下面的C#代码摘录),但我无法在Flatter中找到选项。请导游Flutter Http Post颤振到SAP,flutter,Flutter,我正在尝试使用HTTPPOST将数据从Flatter传输到SAP。我可以毫无问题地获取数据,但post尝试失败,代码403(x-csrf-token无效) 我在使用C#时也遇到了同样的问题,但这是通过使用事件处理程序解决的,它在保存之前触发(请参见下面的C#代码摘录),但我无法在Flatter中找到选项。请导游 zZSSALE1SRVEntity.SendingRequest2 += new EventHandler<SendingRequest2EventArgs>(_con
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));
}
zZSSALE1SRVEntity.SendingRequest2+=新事件处理程序(\u容器\u SendingRequest\u增强);
zzssale1srerventity.SaveChanges();
私有void _容器_发送请求_增强(对象发送方,发送请求2目标)
{
HttpWebResponse;
string empty=string.empty;
string str=string.Empty;
CookieContainer CookieContainer=新CookieContainer();
OdataSsaleDEV.ZZSSALE1_SRV_Entities zzssale1srentity=新OdataSsaleDEV.ZZSSALE1_SRV_Entities(应用程序uri)
{
凭证=凭证
};
字符串str1;
if(empty==string.empty)
{
HttpWebRequest凭据=(HttpWebRequest)WebRequest.Create(zzssale1srerventity.BaseUri);
credentials.Method=“GET”;
credentials.Headers.Add(“X-CSRF-Token”,“Fetch”);
credentials.credentials=zzssale1srerventity.credentials;
cookieContainer=新cookieContainer();
credentials.CookieContainer=CookieContainer;
尝试
{
response=(HttpWebResponse)credentials.GetResponse();
}
捕获(WebException WebException)
{
MessageBox.Show(webException.Message);
返回;
}
捕获(异常)
{
MessageBox.Show(exception.Message);
返回;
}
empty=response.Headers.Get(“X-CSRF-Token”);
str=response.Headers.Get(“Set Cookie”);
cordentials.Abort();
}
if(空!=string.empty)
{
e、 SetHeader(“x-csrf-token”,空);
foreach(cookieContainer.GetCookies中的Cookie cooky(zzsale1srerventity.BaseUri))
{
str1=string.Concat(str1,“;”,cooky.ToString();
}
e、 SetHeader(“Cookie”,str1.Substring(1));
}
问题已解决
实际上,服务器需要会话cookie(mysapso和SAP_SESSIONID)与x-csrf-token一起使用。您可以使用http包。它很容易使用,应该可以解决您的问题。是的,我尝试了所有http、httpclient、dio,我得到了相同的403错误,其中响应体显示x-csrf-token验证失败。我在标头中传递x-csrf-token。我首先调用get方法获取token,然后将其传递给post方法我认为,它的会话问题在于,由于get和post是独立的请求,可能是会话未在服务器端验证,不知道如何解决此问题