Webservice call for .net 3.5 for XML payload

  • Thread starter Thread starter v k b
  • Start date Start date
V

v k b

Guest
Hi All,

i have a webservice URL which takes an XML data as input, this is working in .net 4.0,

how can i make it work for .net 3.5


below is the code which works for .net 4.0

private static void SendXmlToWebService()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\Data.xml");
StringBuilder rawXml = new StringBuilder();
rawXml.Append(GetXmlString(xmlDoc));
PostXml("https://it.api-dev.abcd.com/md/ht/mmi/xml/v1/rules", rawXml.ToString());
}

static string GetXmlString(XmlDocument xmlDoc)
{
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xmlDoc.WriteTo(xw);
return sw.ToString();
}

public static void PostXml(string url, string xml)
{
byte[] bytes = Encoding.UTF8.GetBytes(xml);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "application/xml; charset=utf-8";
request.Accept = "application/xml";
request.Headers["Client_id"] = "ABCD;
request.Headers["Client_secret"] = "XYZ";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
request.KeepAlive = true;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}

try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
string str = sr.ReadToEnd();
StreamWriter sw = new StreamWriter(@"C:\ResponseXML.txt");
sw.WriteLine(str);
sw.Close();
Console.WriteLine(str);

sr.Close();

if (response.StatusCode != HttpStatusCode.OK)
{
string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
Console.WriteLine(message);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}

}


Thanks in advance

Continue reading...
 

Similar threads

Back
Top