模拟发送POST或Get请求

 UTF8Encoding encoding = new UTF8Encoding();
            byte[] data = encoding.GetBytes(requestDataXml.ToString());

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            myRequest.Method = “POST”;
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            Stream newStream = myRequest.GetRequestStream();

            // 发送数据
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            // 返回数据            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            string content = reader.ReadToEnd();

            XElement xe = XElement.Parse(content);
            return xe;
posted @ 2012-04-12 14:12    阅读(1846)  评论(0编辑  收藏  举报