【问题标题】:Get data from asp.net WebService by WebClient通过 WebClient 从 asp.net WebService 获取数据
【发布时间】:2011-06-22 05:37:10
【问题描述】:

是否可以通过 WebClient.DownloadString() 从我的 WebService.asmx/Method 获取数据?或者如何在不向我的项目添加 WebService 引用的情况下仅通过代码获取数据?

【问题讨论】:

    标签: .net asp.net web-services


    【解决方案1】:
    public XmlDocument SubmitDocument1(XmlDocument xDoc, string URL)
    {
        try
        {
            // get the data from the xml document into a byte stream
            Byte[] bdata = System.Text.Encoding.ASCII.GetBytes(xDoc.OuterXml);
            // instantiate a web client
            WebClient wc = new WebClient();
    
            // add appropriate headers
            wc.Headers.Add("Content-Type", "text/xml");
    
            // add default Credentials 
            wc.Credentials = CredentialCache.DefaultCredentials;
    
            // send data to server, and wait for a response           
    
            Byte[] bresp = wc.UploadData(URL, bdata);
    
            // read the responses
            string resp = System.Text.Encoding.ASCII.GetString(bresp);
    
            XmlDocument xresp = new XmlDocument();
            xresp.LoadXml(resp);
    
            // return the xml document response from the server
            return xresp;
        }
        catch
        {
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多