日期:2014-05-18  浏览次数:20796 次

发送HTTP请求,并且获取返回的XML
现在需要一个服务!
定时去访问设定好的网页,
这个网页的作用就是生成XML文档

怎么用实现服务中访问网页,并且获取到返回结果?

各位大侠给小弟出个招~~~~~

------解决方案--------------------
给你写一段假想的客户端请求代码
C# code
var SentMessage = 获取要发送的消息体();
var HttpWebRequest hp = (HttpWebRequest)WebRequest.Create(url);
hp.Method = "POST";
hp.ContentType = "text/xml";
hp.ServicePoint.Expect100Continue = false;
var sentBuff = Encoding.UTF8.GetBytes(SentMessage);
hp.ContentLength = sentBuff.Length;
using (var sw = hp.GetRequestStream())
{
       sw.Write(sentBuff, 0, sentBuff.Length);
       sw.Flush();
       using (var wr = hp.GetResponse())
       {
            using (var sr = new StreamReader(wr.GetResponseStream(), Encoding.UTF8))
            {
                 ReceivedXml = sr.ReadToEnd();
            }
       }
}

------解决方案--------------------
在windows服务中
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“”);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
Console.Write(sr.ReadToEnd());
sr.Close();
response.Close();

System.Net.WebClient wc = new System.Net.WebClient();
wc.Credentials = System.Net.CredentialCache.DefaultCredentials;
Byte[] pageData = wc.DownloadData("");
string html= System.Text.Encoding.Default.GetString(pageData);