webservice调用的两种方法

1。第一中不用说了。使用vs.net提供的工具,生成代理类。

2。使用webrequest的方法。

 

public static XmlDocument XmlSendByHttp(string xmlString, string url, string SoapAction)
        {
            XmlDocument outxml 
= new XmlDocument();
            Stream oWriter 
= null;
            
bool bResult = true;
            
byte[] data = Encoding.UTF8.GetBytes(xmlString);
            HttpWebRequest myRequest 
= (HttpWebRequest)WebRequest.Create(url);

            myRequest.Method 
= "POST";
            myRequest.Headers.Add(
"SOAPAction", SoapAction);    
            myRequest.Headers.Add("ContentLength", data.Length.ToString());
            myRequest.ContentType 
= "text/xml; charset=utf-8";

            
try
            {
                oWriter 
= myRequest.GetRequestStream();
                oWriter.Write(data, 
0, data.Length);
                oWriter.Close();
            }
            
catch
            {
                oWriter.Close();
                bResult 
= false;
            }

            Stream oReader 
= null;
            
if (bResult)
            {
                
try
                {
                    HttpWebResponse oResponse 
= (HttpWebResponse)myRequest.GetResponse();
                    oReader 
= oResponse.GetResponseStream();
                    outxml.Load(oReader);
                    oReader.Close();
                }
                
catch
                {
                }
            }
            
return outxml;
        }
posted @ 2008-12-03 15:12  烈马狂生  阅读(285)  评论(0编辑  收藏  举报