不错博客:
[C#HttpHelper]官方产品发布与源码下载---苏飞版
http://www.sufeinet.com/thread-3-1-1.html
http://blog.csdn.net/xxj_jing/article/details/8107882
http://zhoufoxcn.blog.51cto.com/792419/561934/
http://www.dedecms.com/knowledge/program/asp-donet/2012/0803/4214.html
http://blog.csdn.net/zhoufoxcn/article/details/6404236
C# winform post 请求指定 url
/// <summary>
/// 请求信息帮助
/// </summary>
public class HttpHelper
{
/// <summary>
/// post 请求指定地址返回响应数据
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="postData">请求数据</param>
/// <param name="timeout">请求超时时间(毫秒)</param>
/// <param name="requestCoding">请求编码</param>
/// <param name="responseCoding">响应编码</param>
/// <returns></returns>
public string PostRequest(string url, Dictionary<string, string> postData, int timeout, Encoding requestCoding, Encoding responseCoding)
{
string postStr = string.Empty;
if (postData != null)
{
postData.All(o =>
{
if (string.IsNullOrEmpty(postStr))
postStr = string.Format("{0}={1}", o.Key, o.Value);
else
postStr += string.Format("&{0}={1}", o.Key, o.Value); return true;
});
}
return PostRequest(url, postStr, timeout, requestCoding, responseCoding);
}
/// <summary>
/// post 请求指定地址返回响应数据
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="postData">请求数据</param>
/// <param name="timeout">请求超时时间(毫秒)</param>
/// <param name="requestCoding">请求编码</param>
/// <param name="responseCoding">响应编码</param>
/// <returns></returns>
public string PostRequest(string url, string postData, int timeout, Encoding requestCoding, Encoding responseCoding)
{
string postUrl = VerifyUrl(url);
try
{
//写入POSTDATE里
byte[] byteArray = requestCoding.GetBytes(postData);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webRequest.Method = "POST";
webRequest.Timeout = timeout;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
Stream newStream = webRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
//接收返回信息:
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader php = new StreamReader(response.GetResponseStream(), responseCoding);
string result = php.ReadToEnd();
return result;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 验证URL
/// </summary>
/// <param name="url">待验证 URL</param>
/// <returns></returns>
private string VerifyUrl(string url)
{
if (string.IsNullOrEmpty(url))
throw new Exception("URL 地址不可以为空!"); if (url.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase))
return url; return string.Format("http://{1}", url);
}
}
/// <summary>
/// Http请求
/// </summary>
public class HttpUtility
{
/// <summary>
///通用HttpWebRequest
/// </summary>
/// <param name="method">请求方式(POST/GET)</param>
/// <param name="url">请求地址</param>
/// <param name="param">请求参数</param>
/// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param>
/// <returns>请求返回的结果</returns>
public static string Request(string method, string url, string param, Action<HttpStatusCode, string> onComplete = null)
{
if (string.IsNullOrEmpty(url))
throw new ArgumentNullException("URL"); switch (method.ToUpper())
{
case "POST":
return Post(url, param, onComplete);
case "GET":
return Get(url, param, onComplete);
default:
throw new EntryPointNotFoundException("method:" + method);
}
} /// <summary>
/// Post请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="param">参数</param>
/// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param>
/// <returns>请求返回的结果</returns>
public static string Post(string url, string param, Action<HttpStatusCode, string> onComplete = null)
{
UrlCheck(ref url); byte[] bufferBytes = Encoding.UTF8.GetBytes(param); var request = WebRequest.Create(url) as HttpWebRequest;//HttpWebRequest方法继承自WebRequest, Create方法在WebRequest中定义,因此此处要显示的转换
request.Method = "POST";
request.ContentLength = bufferBytes.Length;
request.ContentType = "application/x-www-form-urlencoded"; try
{
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(bufferBytes, , bufferBytes.Length);
}
}
catch (WebException ex)
{ return ex.Message;
} return HttpRequest(request, onComplete);
} /// <summary>
/// GET请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="param">参数</param>
/// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param>
/// <returns>请求返回结果</returns>
public static string Get(string url, string param, Action<HttpStatusCode, string> onComplete = null)
{
UrlCheck(ref url); if (!string.IsNullOrEmpty(param))
if (!param.StartsWith("?"))
param += "?" + param;
else
param += param; var request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded"; return HttpRequest(request, onComplete);
} /// <summary>
/// 请求的主体部分(由此完成请求并返回请求结果)
/// </summary>
/// <param name="request">请求的对象</param>
/// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param>
/// <returns>请求返回结果</returns>
private static string HttpRequest(HttpWebRequest request, Action<HttpStatusCode, string> onComplete = null)
{
HttpWebResponse response = null; try
{
response = request.GetResponse() as HttpWebResponse;
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
} if (response == null)
{
if (onComplete != null)
onComplete(HttpStatusCode.NotFound, "请求远程返回为空");
return null;
} string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
} if (onComplete != null)
onComplete(response.StatusCode, result); return result; } /// <summary>
/// URL拼写完整性检查
/// </summary>
/// <param name="url">待检查的URL</param>
private static void UrlCheck(ref string url)
{
if (!url.StartsWith("http://") && !url.StartsWith("https://"))
url = "http://" + url;
}
}
C#分析URL参数获取参数和值得对应列表(二)的更多相关文章
-
C#分析URL参数获取参数和值得对应列表
原文: C#分析URL参数获取参数和值得对应列表 /// <summary> /// 分析url链接,返回参数集合 /// </summary> /// <param n ...
-
C#分析URL参数获取参数和值得对应列表(一)
C#操作Url参数 http://www.cnblogs.com/RobotH/archive/2008/11/17/1335322.html 用 C# 分析 URL 中的参数信息 http://ww ...
-
lua post参数获取,参数截断
post 请求头: a.application/x-www-form-urlencoded 普通表单提交 b.multipart/form-data 含有文件的表单,二进制上传 c.applicati ...
-
php分割url,获取参数query
#测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址echo $_SERVER['HTTP_HOST']."<br> ...
-
js 从URL上获取参数
//获取匹配的 function getUrlParam(name) { var reg = new RegExp("(^|&)" + ...
-
从url(地址栏)获取参数:Jquery中getUrlParam()方法的使用
我想要获取如下id 如下代码(传参要加问好!!) function getUrlParam(id) { var regExp = new RegExp('([?]|&)' + id+ '=([ ...
-
JS控制静态页面之间传递参数获取参数并应用
在项目中遇到这也一个问题: 有a.html和b.html. 1.a页面已经打开,b页面尚未打开,我希望在a页面设置好一些列参数,比如背景色,宽度等参数,传递给b页面,好让b页面在打开就能应用. 2.a ...
-
Python股票分析系列——自动获取标普500股票列表.p5
该系列视频已经搬运至bilibili: 点击查看 欢迎来到Python for Finance教程系列的第5部分.在本教程和接下来的几节中,我们将着手研究如何为更多公司提供大量的定价信息,以及如何一次 ...
-
${param.xxx}获取url中的参数
在项目中看到了一个很奇怪的EL表达式...${param.catid}...一直找不到param在哪里定义的...(主要是水平太屎...) 然后从网上查了一下才知道是啥... EL表达式${param ...
随机推荐
-
[置顶]PADS PCB功能使用技巧系列之NO.004- 如何做到20H规则?
电源层与地层之间变化的电场在板边缘会向外辐射电磁干扰(EMI),称为边沿效应.20H规则可将70%的电场限制在接地层边沿内,100H可达到98%. (1)在Layout中,选择菜单栏Setup -&g ...
-
转:Google全程面试题目(未完成)
转自:http://kb.cnblogs.com/page/95951/ 经过了三个月的断断续续的面试和准备,最近一阵抓了很多时间努力准备, 本以为最后的一次面试能弥补前面的不足,可惜还是功亏一篑.. ...
-
Oracle-11g-R2(11.2.0.3.x)RAC Oracle Grid &; Database 零宕机方式回滚 PSU(自动模式)
回滚环境: 1.源库版本: Grid Infrastructure:11.2.0.3.15 Database:11.2.0.3.15 2.目标库版本: Grid Infrastructure:11.2 ...
-
【转】揭秘JavaScript中谜一样的this
原文:http://www.ituring.com.cn/article/66889 在这篇文章里我想阐明JavaScript中的this,希望对你理解this的工作机制有一些帮助.作为JavaScr ...
-
node.js报错throw err; // Rethrow non-MySQL errors e:\serverTest\node_modules\mysql\lib\protocol\Parser.js:79 解决方法
今天在用node+angular做后台时,需要使用session保存登陆状态的时候,遇到了此问题,问题直译为非mysql问题,我也在后台取到的登陆用户名和密码,确实不是数据库问题.最后发现在使用ses ...
-
OpenStack_I版 7.Cinder部署
Cinder提供块存储 Cinder安装 创建相关的目录 创建数据库 修改Cinder配置文件 同步数据库 keystone相关配置 ...
-
Open-sourcing sso, the way we secure services at BuzzFeed
文章来源: https://tech.buzzfeed.com/unleashing-the-a6a1a5da39d6 说明: 设计有好多地方值得借鉴,粘贴过来的排版不好 Today we are o ...
-
【洛谷】【动态规划+单调队列】P1725 琪露诺
[题目描述:] 在幻想乡,琪露诺是以笨蛋闻名的冰之妖精. 某一天,琪露诺又在玩速冻青蛙,就是用冰把青蛙瞬间冻起来.但是这只青蛙比以往的要聪明许多,在琪露诺来之前就已经跑到了河的对岸.于是琪露诺决定到河 ...
-
高可用web框架
nginx nginx简介 Nginx是一个*.开源.高性能及轻量级的HTTP服务器及反转代理服务器.Nginx以其高性能.稳定.功能丰富.配置简单及占用系统资源少而著称. Nginx 超越 Apa ...
-
VS2013只显示会附加到进程,无法启动调试
今天在使用VS2013的时候,打开突然发现,只显示附加到进程,无法进行调试,调试位置显示灰色,到网上各处寻求答案,本以为是个大问题,没想到只是个小问题.主要原因只是后台开太多东西了,导致VS2013运 ...