Greetings All
We are trying to make a POST/PUT request from C# .NET 3.5 client application to SAP Netweaver Gateway;
Background:
- The URL and port numbers are valid - this has been tested using Netweaver client and returns HTTP 200.
- The code we have is able to obtain X-CSRF-Token and Cookie from an initial GET requestion, We add these relevant headers X-CSRF-Token and Set-cookie for the POST request.
- We supply the correct UID and Password accordingly because we are able to obtain the token and cookie from previous get function.
- SAP Netweaver version: 7.40 sp level 9
- Are there any other additional http request parameters required to succesfuly submit a POST or a PUT to SAP Netweaver Gateway.
- Are there any other additional settings from a transaction level that we have to set up to make this service call from external client source.
Please see the following code:
private void CheckCrossSiteForgeryToken()
{
Cursor.Current = Cursors.WaitCursor;
string _UrlToInvoke= "https://xxxxxxxxxxx:4430/sap/opu/odata/SAP/ZTEST_PROJECT_SRV_01/ZTEST_PROJECTSet";
HttpWebResponse httpWebResponse = null;
try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(UrlToInvoke);
httpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
httpRequest.Method = "GET";
httpRequest.KeepAlive = true;
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36";
httpRequest.AllowAutoRedirect = true;
httpRequest.Credentials = new NetworkCredential(txtUID.Text, txtpwd.Text);
httpRequest.KeepAlive = true;
httpRequest.Headers.Add("X-CSRF-Token", "Fetch");
HttpWebResponse resp = (HttpWebResponse)httpRequest.GetResponse();
// Assign values from response to class variables.
gcsrfToken = resp.Headers.Get("X-CSRF-Token");
gsetCookie = resp.Headers.Get("Set-Cookie");
Stream responseStream = resp.GetResponseStream();
}
catch (WebException error)
{
rtOutput.Text = "Error encountered: " + error.ToString();
}
Cursor.Current = Cursors.Default;
}
The above function authenticates the user and pwd succesfully and returns the relevant Token and Cookie, with no issues..!
private void PostDataTOSAPGateway()
{
Cursor.Current = Cursors.WaitCursor;
string _UnloadReportEventURL1 = "https://xxxxxxxxxxx:4430/sap/opu/odata/SAP/ZTEST_PROJECT_SRV_01/ZTEST_PROJECTSet";
HttpWebResponse httpWebResponse = null;
try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(UrlToInvoke);
httpRequest.Accept = "application/xml,application/atom+xml";
httpRequest.Method = "PUT";
httpRequest.ProtocolVersion = HttpVersion.Version11;
httpRequest.AllowWriteStreamBuffering = true;
httpRequest.KeepAlive = true;
//httpRequest.ContentType = "application/x-www-form-urlencoded";
//request.ContentType = "application/json; charset=utf-8";
httpRequest.ContentType = "application/atom+xml";
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36";//"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Mobile/7D11";
httpRequest.AllowAutoRedirect = true;
httpRequest.Credentials = new NetworkCredential(txtUID.Text, txtpwd.Text);
httpRequest.KeepAlive = true;
//set the Token
httpRequest.Headers.Add("X-CSRF-Token", gcsrfToken);
//set the cookie
httpRequest.Headers.Add("Set-Cookie", gsetCookie);
//check the header is applied before sending
gcsrfToken = httpRequest.Headers.Get("X-CSRF-Token");
String fileName = "Test XML.txt";
//String fileName = "Event_single.txt";
string sDir = "C:\\TestData\\";
// Read the data.
StreamReader streamData = new StreamReader(sDir + fileName); //StreamReader(Application.StartupPath + @"/Resources/" + fileName + ".txt"))
//obtain the data to submit
String sxml = streamData.ReadToEnd();
streamData.Close();
streamData.Dispose();
byte[] bytedata = Encoding.UTF8.GetBytes(sxml);
httpRequest.ContentLength = bytedata.Length;
using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(sxml);
streamWriter.Flush();
}
The HTTP Web exception occurs when we try to obtain the response from the above PUT the following:
the exception is HTTP 403.
try
{
httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
}
catch (WebException ex)
{
MessageBox.Show("Exception: " + ex.Message + " " + ex.Response + " status:" + ex.Status + " target:" + ex.TargetSite + " source:" + ex.Source);
}
}
catch (WebException error)
{
rtOutput.Text = "Error encountered: " + error.ToString();
}
Cursor.Current = Cursors.Default;
}
THE DATA WE ARE Submitting:
<?xml version="1.0" encoding="UTF-8"?><atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><atom:content type="application/xml"><m:properties><d:Mandt>022</d:Mandt><d:Pernr>10000000</d:Pernr></m:properties></atom:content></atom:entry>
The HTTP Web exception occurs on the following:
try
{
httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
}
catch (WebException ex)
{
MessageBox.Show("Exception: " + ex.Message + " " + ex.Response + " status:" + ex.Status + " target:" + ex.TargetSite + " source:" + ex.Source);
}
If anyone has succesfully integrated a PUT/POST to SAP Netweaver using .net framework c#, please provide some input on the above mentioned issues.
Thank you and I look forward to your responses.
Kind regards,
--Edwin Ramos