C# 获取Post或Get返回的网页
来源:互联网 发布:windows怎么取消共享 编辑:程序博客网 时间:2024/06/15 07:52
/// <summary>
/// 获取POST返回的网页代码
/// </summary>
/// <param name="url"></param>
/// <param name="body">传递的参数,格式"roleId=1&uid=2"</param>
/// <param name="contentType">post的cotentType填写:"application/x-www-form-urlencoded" soap填写:"text/xml; charset=utf-8"</param>
/// <returns></returns>
public static string PostHttp(string url, string body = "", string contentType = "application/x-www-form-urlencoded")
{
System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
httpWebRequest.ContentType = contentType;
httpWebRequest.Method = "POST";
httpWebRequest.Timeout = 20000;
byte[] btBodys = System.Text.Encoding.UTF8.GetBytes(body);
httpWebRequest.ContentLength = btBodys.Length;
httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
System.IO.StreamReader streamReader = new System.IO.StreamReader(httpWebResponse.GetResponseStream());
string responseContent = streamReader.ReadToEnd();
httpWebResponse.Close();
streamReader.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return responseContent;
}
/// <summary>
/// 获取Get返回的网页代码
/// </summary>
/// <param name="url"></param>
/// <param name="httpContext">传递的参数,格式"roleId=1&uid=2"</param>
/// <returns></returns>
public static string GetHttp(string url, HttpContext httpContext)
{
string queryString = "?";
if (httpContext != null)
{
foreach (string key in httpContext.Request.QueryString.AllKeys)
{
queryString += key + "=" + httpContext.Request.QueryString[key] + "&";
}
}
queryString = queryString.Substring(0, queryString.Length - 1);
System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url + queryString);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
httpWebRequest.Timeout = 20000;
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)";
//byte[] btBodys = Encoding.UTF8.GetBytes(body);
//httpWebRequest.ContentLength = btBodys.Length;
//httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
System.IO.StreamReader streamReader = new System.IO.StreamReader(httpWebResponse.GetResponseStream());
string responseContent = streamReader.ReadToEnd();
httpWebResponse.Close();
streamReader.Close();
return responseContent;
}
/// 获取POST返回的网页代码
/// </summary>
/// <param name="url"></param>
/// <param name="body">传递的参数,格式"roleId=1&uid=2"</param>
/// <param name="contentType">post的cotentType填写:"application/x-www-form-urlencoded" soap填写:"text/xml; charset=utf-8"</param>
/// <returns></returns>
public static string PostHttp(string url, string body = "", string contentType = "application/x-www-form-urlencoded")
{
System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
httpWebRequest.ContentType = contentType;
httpWebRequest.Method = "POST";
httpWebRequest.Timeout = 20000;
byte[] btBodys = System.Text.Encoding.UTF8.GetBytes(body);
httpWebRequest.ContentLength = btBodys.Length;
httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
System.IO.StreamReader streamReader = new System.IO.StreamReader(httpWebResponse.GetResponseStream());
string responseContent = streamReader.ReadToEnd();
httpWebResponse.Close();
streamReader.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return responseContent;
}
/// <summary>
/// 获取Get返回的网页代码
/// </summary>
/// <param name="url"></param>
/// <param name="httpContext">传递的参数,格式"roleId=1&uid=2"</param>
/// <returns></returns>
public static string GetHttp(string url, HttpContext httpContext)
{
string queryString = "?";
if (httpContext != null)
{
foreach (string key in httpContext.Request.QueryString.AllKeys)
{
queryString += key + "=" + httpContext.Request.QueryString[key] + "&";
}
}
queryString = queryString.Substring(0, queryString.Length - 1);
System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url + queryString);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
httpWebRequest.Timeout = 20000;
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)";
//byte[] btBodys = Encoding.UTF8.GetBytes(body);
//httpWebRequest.ContentLength = btBodys.Length;
//httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
System.IO.StreamReader streamReader = new System.IO.StreamReader(httpWebResponse.GetResponseStream());
string responseContent = streamReader.ReadToEnd();
httpWebResponse.Close();
streamReader.Close();
return responseContent;
}
0 0
- C# 获取Post或Get返回的网页
- C# get或者post获取网页内容
- 通过get或post请求URL获取返回数据
- C#请求网页(Get、Post)
- C#请求网页(Get、Post)
- QT POST/GET 发送/获取网页数据
- QT POST/GET 发送/获取网页数据
- lua获取get或post参数
- #nodejs post或get 请求获取参数
- c#使用post,get请求网页
- c#通过GET/POST获取页面的代码
- C#使用HttpWebRequest POST或GET信息
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- C#使用GET、POST请求获取结果
- 《大话设计模式》讨论
- CUDA driver version is insufficient for CUDA runtime version
- 这是我在南航读研第一年末,写下了第一篇博客
- JAVA 消息队列的使用场景
- 【Anaconda】conda环境管理和包管理
- C# 获取Post或Get返回的网页
- Android系统启动过程
- 2017.4.17
- Android 新特性整理
- SuperMap iCloudManager OpenStack平台部署指南
- MyBatis嵌套查询解析
- android 自带的design包中tablayout使用
- 使用FastJSON,将对象或数组和JSON串互转
- ZOJ Problem Set