HttpWebRequest request = null;
WebResponse response = null;
Try
{
String myurl = "https://blah.blah.gov/themagicform.cfm"; The URL for my requests:
Request = (HttpWebRequest) webrequest.create (Myurl); Create a request using a URL that can receive a post.
Request. Credentials = new NetworkCredential ("MyUserName", "MyPassword"); The credentials for the website.
Request. method = "POST"; The Set the method property is the request to POST.
Request. ContentType = "Text/xml"; Set the ContentType property of the WebRequest.
Request. ContentType = "application/x-www-form-urlencoded";
Specify Headers
Request. Headers.add ("Http_host", "MyZone");
Request. Headers.add ("Http_user_agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.2. NET CLR 1.1.4322);
Request. Headers.add ("Http_accept", "Text/xml");
StreamWriter writer = new StreamWriter (request. GetRequestStream ()); Wrap the request stream with a text-based writer
Writer. WriteLine (xmldoc); Write the XML text into the stream
Writer. Close ();
/* Previously I tried:
string postdata = xmldoc; Create POST data and convert it to a byte array.
byte[] ByteArray = Encoding.UTF8.GetBytes (postdata);
Request. ContentLength = Bytearray.length; Set the ContentLength property of the WebRequest.
Stream DataStream = Request. GetRequestStream (); Get the request stream.
Datastream.write (ByteArray, 0, bytearray.length); Write the data to the request stream.
Datastream.close (); Close the Stream Object-which submits the "form" data.
*/
Stream dataStream = null;
Send the data to the webserver
Response = Request. GetResponse (); Get the response.
Lbl_responseinfo.text = ((httpwebresponse) response). Statusdescription.tostring (); Display the status.
DataStream = Response. GetResponseStream (); Get the stream containing content returned by the server.
StreamReader Incomingstreamreader = new StreamReader (dataStream); The Open the stream using a StreamReader for easy access.
String responsefromserver = Incomingstreamreader.readtoend ()//Read the content.
Display the content. Xmlresponse is a special type of label on my ASP page
Xmlresponse.visible = true;
Xmlresponse.transformsource = "Defaultss.xslt";
Xmlresponse.documentcontent = Responsefromserver;
Lbl_received.visible = true;
Clean up the streams.
Incomingstreamreader.close ();
Datastream.close ();
Response. Close ();
Display the XML we sent
Xmlsent.visible = true;
Xmlsent.transformsource = "Defaultss.xslt";
Xmlsent.documentcontent = xmldoc;
Lbl_sent.visible = true;
}
catch (WebException webEx)
{
Lbl_responseinfo.text = WebEx.Message.ToString ();
}
catch (Exception ex)
{
Lbl_responseinfo.text = ex. Message.tostring ();
}
Finally
{
if (Request!= null) request. GetRequestStream (). Close ();
if (response!= null) response. GetResponseStream (). Close ();
}