Quote:
Originally Posted by surendran
i have to post a xml file into a 3rd party server andneed to get the feed abck from them, here is the code I used, but no response code included, please give me a sample
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
namespace WebApplication1
{
publicpartialclass_Default : System.Web.UI.Page
{
protectedvoid abc()
{
WebRequest req = null;
WebResponse rsp = null;
try
{
string fileName = "C:/test.txt";
string uri = "https://www.ntorder-dev.com/ocs-nisa/B2B/";
req = WebRequest.Create(uri);
//req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
req.Method = "POST"; // Post method
req.ContentType = "text/xml"; // content type
// Wrap the request stream with a text-based writer
StreamWriter writer = newStreamWriter(req.GetRequestStream());
// Write the XML text into the stream
writer.WriteLine(this.GetTextFromXMLFile(fileName));
writer.Close();
// Send the data to the webserver
rsp = req.GetResponse();
}
catch (WebException webEx)
{
}
catch (Exception ex)
{
}
finally
{
if (req != null) req.GetRequestStream().Close();
if (rsp != null) rsp.GetResponseStream().Close();
}
}
protectedvoid Page_Load(object sender, EventArgs e)
{
abc();
this.Response.ContentType = "text/xml";
// Read XML posted via HTTP
StreamReader reader = newStreamReader(this.Request.InputStream);
String xmlData = reader.ReadToEnd();
}
privatestring GetTextFromXMLFile(string file)
{
StreamReader reader = newStreamReader(file);
string ret = reader.ReadToEnd();
reader.Close();
return ret;
}
}
}
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.com");
request.KeepAlive = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
while (!readStream.EndOfStream)
{
Debug.WriteLine(readStream.ReadLine());
}
readStream.Close();
response.Close();