zoukankan      html  css  js  c++  java
  • post xml using HttpWebRequest/Response dodo

    For those of you scouring the web looking for a simple routine that sends/receives an XML file using HttpWebRequest/Response here ya go:

        
    public static XmlDocument PostXMLTransaction(string v_strURL, XmlDocument v_objXMLDoc)
        {
            
    //Declare XMLResponse document
            XmlDocument XMLResponse = null;

            
    //Declare an HTTP-specific implementation of the WebRequest class.
            HttpWebRequest objHttpWebRequest;

            
    //Declare an HTTP-specific implementation of the WebResponse class
            HttpWebResponse objHttpWebResponse = null;

            
    //Declare a generic view of a sequence of bytes
            Stream objRequestStream = null;
            Stream objResponseStream 
    = null;

            
    //Declare XMLReader
            XmlTextReader objXMLReader;

            
    //Creates an HttpWebRequest for the specified URL.
            objHttpWebRequest = (HttpWebRequest)WebRequest.Create(v_strURL);

            
    try
            {
                
    //---------- Start HttpRequest 

                
    //Set HttpWebRequest properties
                byte[] bytes;
                bytes 
    = System.Text.Encoding.ASCII.GetBytes(v_objXMLDoc.In nerXml);
                objHttpWebRequest.Method 
    = "POST";
                objHttpWebRequest.ContentLength 
    = bytes.Length;
                objHttpWebRequest.ContentType 
    = "text/xml; encoding='utf-8'";

                
    //Get Stream object 
                objRequestStream = objHttpWebRequest.GetRequestStream();

                
    //Writes a sequence of bytes to the current stream 
                objRequestStream.Write(bytes, 0, bytes.Length);

                
    //Close stream
                objRequestStream.Close();

                
    //---------- End HttpRequest

                
    //Sends the HttpWebRequest, and waits for a response.
                objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();

                
    //---------- Start HttpResponse
                if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    
    //Get response stream 
                    objResponseStream = objHttpWebResponse.GetResponseStream();

                    
    //Load response stream into XMLReader
                    objXMLReader = new XmlTextReader(objResponseStream);

                    
    //Declare XMLDocument
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(objXMLReader);

                    
    //Set XMLResponse object returned from XMLReader
                    XMLResponse = xmldoc;

                    
    //Close XMLReader
                    objXMLReader.Close();
                }

                
    //Close HttpWebResponse
                objHttpWebResponse.Close();
            }
            
    catch (WebException we)
            {
                
    //TODO: Add custom exception handling
                throw new Exception(we.Message);
            }
            
    catch (Exception ex)
            {
                
    throw new Exception(ex.Message);
            }
            
    finally
            {
                
    //Close connections
                objRequestStream.Close();
                objResponseStream.Close();
                objHttpWebResponse.Close();

                
    //Release objects
                objXMLReader = null;
                objRequestStream 
    = null;
                objResponseStream 
    = null;
                objHttpWebResponse 
    = null;
                objHttpWebRequest 
    = null;
            }

            
    //Return
            return XMLResponse;
        }
  • 相关阅读:
    对于.net config文件中加密使用
    删除windows不想要的服务
    ora12514: tns: 监听程序当前无法识别连接描述符中请求的服务
    读《.NET设计规范》笔记
    创建服务总结
    加载XML文档
    XMLHttpRequest对象的使用
    ReportView控件的使用
    C#代码的编译过程
    Qt vs设置可执行程序图标
  • 原文地址:https://www.cnblogs.com/zgqys1980/p/1681200.html
Copyright © 2011-2022 走看看