Monday, April 6, 2009

XML transfer between two interface( like: Sharepoint to SAP )

Some time back, I got a requirement in my project. We need to send a xml file from sharepoint to SAP system and get response back from them. Note: This transaction can be done between any two application.
I thought what would be easiest and best approach. Hmmm, lets try with basic HTTP Post/Get method to do that. As in sharepoint I can use compile .NET code so no problem for me to go for it.

.NET Code to do that:

WebRequest req = null;
WebResponse rsp = null;
try
{
string fileName = @"D:\Projects\abc.xml";
string uri = "http://localhost/sap/test/test.htm";
req = WebRequest.Create(uri);
//req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
req.Method = "POST"; // Post method
req.ContentType = "text/xml"; // content type
//If end user doesnot has rights to update than pass required credential explicitly
req.Credentials= CredentialCache.DefaultCredentials;
req.Credentials = new NetworkCredential("abc", "xyz");
// Wrap the request stream with a text-based writer
StreamWriter writer = new StreamWriter(req.GetRequestStream());
//read the xml file
StreamReader reader = new StreamReader(fileName);
string ret = reader.ReadToEnd();
reader.Close();
// Write the XML text into the stream
writer.WriteLine(ret);
writer.Close();
// Send the data to the webserver
rsp = req.GetResponse();

//Get response from server; ex: two parameters is passed from receiving server
string strContents, strContents1 = "";
strmContents = rsp.Headers["test1"].ToString();
strmContents1 = rsp.Headers["test2"].ToString();
}

No comments: