XML 통신

1.ASP.NET XML 전송 후 응답받기





 
1
2
3
4
5
6
7
8
9
10
11
        public ActionResult PostTest()
        {
            XmlDocument xmlObj = new XmlDocument();
            string xmlStr = "<Send><UserName>Colt</UserName><PassWord>Taylor</PassWord><Data>100</Data></Send>";
            xmlObj.LoadXml(xmlStr);
            string reqUrl = "http://hope20603-001-site1.smarterasp.net/xmlreq.asp";
            PostXMLTransaction(reqUrl, xmlObj);
 
            return View();
        }
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/// <summary>
        /// xml 통신
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <returns></returns>
        public static XmlDocument PostXMLTransaction(string v_strURL, XmlDocument v_objXMLDoc)
        {
            XmlDocument XMLResponse = null;
            HttpWebRequest objHttpWebRequest;
            HttpWebResponse objHttpWebResponse = null;
            Stream objRequestStream = null;
            Stream objResponseStream = null;
            XmlTextReader objXMLReader;
 
            objHttpWebRequest = (HttpWebRequest)WebRequest.Create(v_strURL);
 
            try
            {
                
 
                
                byte[] bytes;
                bytes = System.Text.Encoding.ASCII.GetBytes(v_objXMLDoc.InnerXml);
                objHttpWebRequest.Method = "POST";
                objHttpWebRequest.ContentLength = bytes.Length;
                objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
                
                objRequestStream = objHttpWebRequest.GetRequestStream();
                objRequestStream.Write(bytes, 0, bytes.Length);
                objRequestStream.Close();
 
                objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
 
                if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                
                    objResponseStream = objHttpWebResponse.GetResponseStream();
                    objXMLReader = new XmlTextReader(objResponseStream);
                    
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(objXMLReader);
                    XMLResponse = xmldoc;
                    string response = XMLResponse.InnerText;
                    objXMLReader.Close();
                }
                
                objHttpWebResponse.Close();
            }
            catch (WebException we)
            {
                throw new Exception(we.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                
                objRequestStream.Close();
                objResponseStream.Close();
                objHttpWebResponse.Close();
                
                objXMLReader = null;
                objRequestStream = null;
                objResponseStream = null;
                objHttpWebResponse = null;
                objHttpWebRequest = null;
            }
            
            return XMLResponse;
        }
cs

2. ASP XML데이터 받은 후 리턴

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
<%
    Dim xmlDoc
    Dim userName
    set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
    xmlDoc.async="false"
    xmlDoc.load(Request)
    userName = xmlDoc.documentElement.selectSingleNode("UserName").firstChild.nodeValue
 
 
    response.contenttype="text/xml"
    response.write("<?xml version='1.0' encoding='euc-kr'?>")
    response.write("<booking_info xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>")
    response.write("    <error>[ "& userName &" ] </error>")
    response.write("</booking_info>")
 
 
%>
cs


+ Recent posts