C# post using xcsrf token



I am trying to post xml data using c# fetching x-csrf-token


Am able to fetch token using get method and when I try to post I am getting 403 error. Please assist me if I am wrong





string Token = "";
public string Postdata = "<?xml version='1.0' encoding='utf-8'?> <entry xml:base='http://server:8000/sap/opu/odata/sap/ZCUST_TESTING_SRV/' xmlns='http://ift.tt/r5ut6F' xmlns:m='http://ift.tt/1dtZymv' xmlns:d='http://ift.tt/1cVQIMf'> <content type='application/xml'><m:properties><d:Kunnr>55556697</d:Kunnr><d:Land1>abi</d:Land1><d:Name1>abh</d:Name1><d:Name2>chaitanya</d:Name2><d:Ort01>kennedy</d:Ort01><d:Pstlz>500060</d:Pstlz><d:EStatus>X</d:EStatus></m:properties></content></entry>";
public string URL = "http://server:8000/sap/opu/odata/sap/ZCUST_TESTING_SRV/post_cust";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Credentials = new NetworkCredential("username","password");
request.ContentType = "application/atom+xml";
request.Accept = "application/xml,application/atom+xml";
request.Method = "GET";
request.Headers["x-csrf-token"] = "Fetch";
HttpWebResponse responseH = (HttpWebResponse)request.GetResponse();
Token = responseH.Headers["x-csrf-token"];
//xcookies = responseH.Headers["set-cookie"];
responseH.Close();
// Here I'm creating Post Method ...
string requestString = HttpUtility.UrlPathEncode(data); //XMLDoc is the XML data string being submitted.
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(postdata);
HttpWebRequest request_post = (HttpWebRequest)WebRequest.Create(method);
request_post.Credentials = new NetworkCredential("username", "password");
request_post.Method = "POST";
request_post.ContentType = "application/xml";
request_post.Accept = "application/xml";
request_post.Headers["x-csrf-token"] = Token;
request_post.ContentLength = byteArray.Length;
request_post.KeepAlive = false;
Stream dataStream1 = request_post.GetRequestStream();
dataStream1.Write(byteArray, 0, byteArray.Length);
dataStream1.Close();
HttpWebResponse response1 = (HttpWebResponse)request_post.GetResponse();
dataStream1 = response1.GetResponseStream();
StreamReader reader1 = new StreamReader(dataStream1);
string responseFromServer1 = reader1.ReadToEnd();
reader1.Close();
dataStream1.Close();
response1.Close();




No comments:

Post a Comment