HTML传输方法(只有Post和Get)
2015-06-16 17:08
429 查看
稍微修改了点
public static string CollectHtml(string targetUrl, string sendContent, RequestParameters reqParam)
{
string html = string.Empty;
Stream stream = null;
StreamReader sr = null;
Stream reqStream = null;
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
byte[] bs = Encoding.GetEncoding(GetEnumDescription<EncodingType>(reqParam.sendEncoding)).GetBytes(sendContent);
switch (reqParam.reqMethod)
{
case ReqMethod.Post:
request = (HttpWebRequest)WebRequest.Create(targetUrl);
request.Method = reqParam.reqMethod.ToString();
request.ContentLength = bs.Length;
request.UserAgent = reqParam.reqUserAgent;
request.ContentType = GetEnumDescription<ReqContentType>(reqParam.reqContentType);
request.Accept = reqParam.reqAccept;
break;
case ReqMethod.Get:
request = (HttpWebRequest)WebRequest.Create(targetUrl + (sendContent == "" ? "" : "?") + sendContent);
request.Method = reqParam.reqMethod.ToString();
request.ContentType = reqParam.reqContentType.ToString();
break;
default:
break;
}
if (bs.Length > 0 && reqParam.reqMethod == ReqMethod.Post)
{
reqStream = request.GetRequestStream();
reqStream.Write(bs, 0, bs.Length);
}
if (request != null)
response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
sr = new StreamReader(stream, Encoding.GetEncoding(GetEnumDescription<EncodingType>(reqParam.receivEncoding)));
html = sr.ReadToEnd();
}
catch (WebException wEx)
{
response = (HttpWebResponse)wEx.Response;
stream = response.GetResponseStream();
sr = new StreamReader(stream, Encoding.GetEncoding(GetEnumDescription<EncodingType>(reqParam.receivEncoding)));
html = sr.ReadToEnd();
}
finally
{
if (response != null)
{
response.Close();
}
if (stream != null)
{
stream.Dispose();
}
if (sr != null)
{
sr.Dispose();
}
if (reqStream != null)
{
reqStream.Dispose();
}
if (request != null)
{
request.Abort();
}
}
return html;
}
public struct RequestParameters
{
public EncodingType sendEncoding
{ get; set; }
public EncodingType receivEncoding
{ get; set; }
public ReqMethod reqMethod
{ get; set; }
public string reqUserAgent
{
get { return "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)"; }
}
public ReqContentType reqContentType
{ get; set; }
public string reqAccept
{
get { return "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*"; }
}
}
public enum EncodingType
{
GB2312,
GBK,
[Description("UTF-8")]
UTF8
}
public enum ReqMethod
{
Post, Get
}
public enum ReqContentType
{
[Description("application/x-www-form-urlencod")]
Default,
[Description("image/jpeg")]
Image,
[Description("application/json")]
Json,
[Description("text/plain")]
Text,
[Description("text/xml")]
Xml
}
相关文章推荐
- HTML表格
- HTML框架与表单
- 将HTML格式的String转化为HTMLElement
- html 水平 垂直 居中简易实现
- HTML常用标签
- html解析器:Html Agility Pack
- word转html
- HTML 5 服务器发送事件
- 用meteor开发网页时html中的seletc 默认选项的设置
- html的meta用法详解
- 在开发Ext 项目中如果遇到 Uncaught TypeError: Cannot read property 'insertAdjacentHTML' of null
- html的meta标签解释
- angular中的ng-bind-html指令和$sce服务
- (五)html部分标签元素补充
- HTML attribute
- Flask在根目录'/'下返回静态HTML模版
- HTML格式自定义OpenCart邮件模板功能插件
- HTML格式自定义OpenCart邮件模板功能插件
- HTML meta头部小结
- Html.TextBoxFor三元判断