I need to Post the XML data to a URL.But I am facing some Error in the (Stream requestStream = request.GetRequestStream()) Line Errror Image . I have saved the xml in the file and provided the path for that . I think there is Issue while passing XML Data but when I try to use the same XML in the POSTMan using POSTmethod.I can able to get the response Data.
This is the reference link HTTP post XML data in C#
And here is my code
static void Main(string[] args)
{
string url = "*****************";
XmlDocument Xdoc = new XmlDocument();
Xdoc.Load(@"Path of the xml file");
var sw = new StringWriter();
Xdoc.Save(sw);
string result = sw.ToString();
string response = **********.postXMLData(url, result);
Console.WriteLine(response);
Console.ReadLine();
}
public static string postXMLData(string destinationUrl, String requestXml)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
request.ContentType = "text/xml; encoding=utf-8";
request.ContentLength = bytes.Length;
request.Method = "POST";
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
return responseStr;
}
return null;
}