0
Dim doc As New XmlDocument
doc.Load("http://www.example.com/?paramx=1&paramy=2")

非常适合查询字符串中的 GET 查询。但是假设我想将 paramx=1¶my=2 发布到http://www.example.com 并以 XML 格式获取响应。我该怎么做?

4

1 回答 1

0

下面是一个帮助类,我用于将数据发布到网站,然后返回 XML - 希望它有所帮助。

用法:

var resultXmlDoc = new HttpHelper().PostXml("http://www.test.com", new { paramName = "value", paramName2="value2" });

助手类:

internal class HttpHelper
{

    public XDocument PostXml(string baseUrl, IDictionary<string, string> cgiParams)
    {
        return XDocument.Load(Post(baseUrl, cgiParams).GetResponseStream());
    }

    public XDocument PostXml(string baseUrl, params object[] items)
    {
        return XDocument.Load(Post(baseUrl, items).GetResponseStream());
    }

    public XDocument PostXml(string url, string topost)
    {
        return XDocument.Load(Post(url, topost).GetResponseStream());
    }

        protected virtual HttpWebResponse Post(string url, string postString)
        {
            if (url == null)
                throw new ArgumentNullException("baseUrl");

            Uri uri;
            if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
                throw new ArgumentException("url is not a valid Uri");


            var req = (HttpWebRequest)WebRequest.Create(url);
            // if the target site issues a session cookie, you need to store it and append it here
            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";
            var bytes = System.Text.Encoding.ASCII.GetBytes(postString);
            req.ContentLength = bytes.Length;

            using(var str = req.GetRequestStream())
                str.Write(bytes, 0, bytes.Length);


            return (HttpWebResponse)req.GetResponse();
        }

        protected virtual HttpWebResponse Post(string url, params object[] items)
        {
            var x = items.SelectMany(i =>
            {
                return i.GetType().GetProperties().Select(p => new Tuple<string, string>(p.Name, p.GetValue(i, null) != null ? p.GetValue(i, null).ToString() : ""));
            });

            var d = new Dictionary<string, string>();
            foreach (var p in x)
                d[p.Item1] = p.Item2;

            return Post(url, d);
        }


        protected virtual HttpWebResponse Post(string baseUrl, IDictionary<string, string> cgiParams)
        {
            if (baseUrl == null)
                throw new ArgumentNullException("baseUrl");

            Uri uri;
            if (!Uri.TryCreate(baseUrl, UriKind.Absolute, out uri))
                throw new ArgumentException("baseUrl is not a valid Uri");


            string postString = string.Empty;
            if (cgiParams != null && cgiParams.Count > 0)
            {
                foreach (var k in cgiParams.Keys)
                    postString += HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(cgiParams[k]) + "&";

                postString = postString.Substring(0, postString.Length - 1);
            }

            return Post(baseUrl, postString);
        }
}
于 2013-05-01T14:40:37.467 回答