Web client Application in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to create a web client code in C# that retrieves data from a web site. In VB I do the following
Set xmlHttp = WScript.CreateObject("Microsoft.XMLHTTP"
Set xmlDom = WScript.CreateObject("Microsoft.XMLDOM"
xmlDom.LoadXml("<DETAILS FirstName=""John"" LastName=""James"" />"
xmlHttp.Open "POST", "http://host/rocet/Purschase.aspx", 0
xmlHttp.Send(xmlDom

A pointer to equivalent process in C# will be appreciated
Ola
 
Just use HttpWebRequest ... works very well ... brief (and incomplete)
example follows :


StringBuilder formatData = new StringBuilder();

//put your post values into the string builder

HttpWebRequest http = (HttpWebRequest)WebRequest.Create(addr);

http.ContentType = "application/x-www-form-urlencoded";

http.ContentLength = formatData.Length;

http.Method = "POST";


Stream strWrite = http.GetRequestStream();

StreamWriter sw = new StreamWriter(strWrite);

sw.Write(formatData.ToString());

sw.Close();

//Now we need to read the data

WebResponse wr = http.GetResponse();

HttpWebResponse httpRes = (HttpWebResponse)wr;

Stream s = httpRes.GetResponseStream();

StreamReader sr = new StreamReader(s,Encoding.ASCII);

ret = sr.ReadToEnd();

sr.Close();

//Debug.WriteLine(ret)



Ola said:
I need to create a web client code in C# that retrieves data from a web
site. In VB I do the following:
 
What about sending the request in XML and receiving response in XML instead of string data?
 
Just change your contentType to 'text/xml' and take the returned Stream and
load it into a XmlTextReader and you'll have your xml doc.

Alex


Ola said:
What about sending the request in XML and receiving response in XML
instead of string data?
 
Thanks Alex. Couldn't figure out strWrite in your example. Please help clarify - Just learning
StringBuilder postData = new StringBuilder("<CABDETAILS CabNumber='43877' CabSKU='6633' ExpDate='0106' Postal='78052' />")
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("http://Cabplace/Purchase.aspx")
Request.Method="POST"
Request.ContentType="text/xml"
Request.ContentLength = postData.Length
Stream newStream=Request.GetRequestStream()
StreamWriter sw = new StreamWriter(strWrite)
sw.Write(postData.ToString())
sw.Close();
 
Ola,

In my example, strWrite was the returned 'Stream' object from the call to
the static Request.GetRequestStream().

In your example, strWrite is actually 'newStream'.

Stream newStream=Request.GetRequestStream();
StreamWriter sw = new StreamWriter(newStream); //this is how that line
should look
sw.Write(postData.ToString());
sw.Close();

Good luck!

Alex


Ola said:
Thanks Alex. Couldn't figure out strWrite in your example. Please help clarify - Just learning.
StringBuilder postData = new StringBuilder("<CABDETAILS CabNumber='43877'
CabSKU='6633' ExpDate='0106' Postal='78052' />");
 
Back
Top