C#分析URL参数获取参数和值得对应列表(二)
不错博客:
[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 ...
随机推荐
- 【CF1023F】Mobile Phone Network(dsu,MST)
题意: 保证原边以边权单调非减的顺序读入 思路:先把未知边加入,再加入原始边做MST,考虑从大到小,用数据结构维护,每一条原始边相当两个链赋值操作,每一条未知边相当于一个询问,答案即为询问之和 LCT ...
- php 中foreach比for快之原因
这里首先要谈到php 的hashtabletypedef struct _hashtable { uint nTableSize; // hash Bucket的大小,最小为8,以2x增长. uint ...
- hdu 4506 小明系列故事——师兄帮帮忙
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4506 题目大意:找规律,判断k的t次幂前面的系数. #include <iostream> ...
- Oracle基础 09 概要文件 profile
--创建 profile 概要文件create profile profile123 limit failed_login_attempts 2; --修改用户的 profile 文件alter u ...
- redis SCAN cursor
https://redis.io/commands/scan 可以看到: sscan的返回结果,有两部分, 第一部分 是一个数字.其实代表一个游标. 第二部分 是结果. scan是以游标为基础,每 ...
- springboot hystrix turbine 聚合监控
1.新建Module eureka-monitor-client 2.父级pom中添加module 3.编写eureka-monitor-client 中的pom <?xml version= ...
- (十五)mysql中间件MyCAT实现
1)拓扑如下和实现目标 写操作:都在master 读操作:在slave1上 当master1挂了,写操作自动切换到master2上 当master2挂了,写操作自动切换到master1上 2)Myca ...
- Square Number & Cube Number
Square Number: Description In mathematics, a square number is an integer that is the square of an in ...
- MySQL的数据类型和建库策略详解
无论是在小得可怜的免费数据库空间或是大型电子商务网站,合理的设计表结构.充分利用空间是十分必要的.这就要求我们对数据库系统的常用数据类型有充分的认识.下面我就将我的一点心得写出来跟大家分享. 一.数字 ...
- Naming conventions of python
1.package name 全部小写字母,中间可以由点分隔开,作为命名空间,包名应该具有唯一性,推荐采用公司或组织域名的倒置,如com.apple.quicktime.v2 2.module nam ...