C# to POST HTTP with XML

本文深入探讨了HTTP POST请求的创建、发送、接收过程,包括设置URL、认证、请求方法、头信息、数据写入及响应处理。通过示例展示了如何使用C#进行HTTP POST请求,包括设置数据格式、头信息、读取服务器响应等内容。

            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();

            }

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值