码迷,mamicode.com
首页 > Web开发 > 详细

HttpWebRequest 和HttpWebResponse总结

时间:2015-04-17 20:10:05      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

1. 总结

 

技术分享

 

总结2

技术分享

 

3. Code

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Web;

namespace Core
{
    public class RequestHelper
    {
        private Stream SendGetRequest1(string url)
        {
            string content;
            //HttpRequest request;//     Enables ASP.NET to read the HTTP values sent by a client during a Web request.
            WebRequest webrequest;//     Makes a request to a Uniform Resource Identifier (URI). This is an abstract
            HttpWebRequest httpWebRequest;//     Provides an HTTP-specific implementation of the System.Net.WebRequest class.
            HttpWebResponse httpWebResponse;
            //string url = "fsefsf";
            httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
            httpWebRequest.AllowAutoRedirect = true;
            httpWebRequest.Method = "GET";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";

            //httpwebrequest.ContentType = "application/json";
            //httpwebrequest.ContentType = "application/xml";
            //httpwebrequest.Headers.Add("url",url);

            httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            Stream resStream = httpWebResponse.GetResponseStream();
            using (StreamReader sr = new StreamReader(resStream))
            {
                content = sr.ReadToEnd();
            }
            if (httpWebResponse.StatusCode != HttpStatusCode.OK)
            {

            }
            return resStream;
        }
        private Stream SendGetRequestForStream(string url, string contentType)
        {
            HttpWebRequest httpWebRequest;//     Provides an HTTP-specific implementation of the System.Net.WebRequest class.
            HttpWebResponse httpWebResponse;
            httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
            httpWebRequest.Method = "GET";
            httpWebRequest.ContentType = contentType;
            httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            Stream resStream = httpWebResponse.GetResponseStream();
            return resStream;
        }


        private String SendGetRequest(string url, string contentType)
        {
            string content = "";
            HttpWebRequest httpWebRequest;
            HttpWebResponse httpWebResponse;
            httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
            httpWebRequest.Method = "GET";
            httpWebRequest.ContentType = contentType;//"application/x-www-form-encoded";
            httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            Stream resStream = httpWebResponse.GetResponseStream();
            using (StreamReader sr = new StreamReader(resStream))
            {
                content = sr.ReadToEnd();
            }
            return content;
        }

        private Stream SendPostRequestForStream(string url, string data, string contentType)
        {
            //string content = "";
            HttpWebRequest httpWebRequest;
            HttpWebResponse httpWebResponse;
            httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = contentType;
            Stream reqStream = httpWebRequest.GetRequestStream();
            using (StreamWriter sw = new StreamWriter(reqStream))
            {
                sw.Write(data);
            }
            httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            Stream ResStream = httpWebResponse.GetResponseStream();

            return ResStream;
        }


        private String SendPostRequest(string url, string data, string contentType)
        {
            string content = "";
            HttpWebRequest httpWebRequest;
            HttpWebResponse httpWebResponse;
            httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = contentType;
            Stream reqStream = httpWebRequest.GetRequestStream();
            using (StreamWriter sw = new StreamWriter(reqStream))
            {
                sw.Write(data);
            }
            httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            Stream resStream = httpWebResponse.GetResponseStream();
            using (StreamReader sr = new StreamReader(resStream))
            {
                content = sr.ReadToEnd();
            }

            return content;
        }
    }
}
View Code

 

HttpWebRequest 和HttpWebResponse总结

标签:

原文地址:http://www.cnblogs.com/slu182/p/4435573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!