C# to POST HTTP with XML

2023-10-29

            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";  // Set the Method property of 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); // 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();

            }

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# to POST HTTP with XML 的相关文章

随机推荐