通过WebClient从asp.net WebService获取数据

时间:2011-06-22 05:37:11

标签: .net asp.net web-services

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

1 个答案:

答案 0 :(得分:0)

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
    {
    }
}