利用HttpWebRequest模拟表单提交 JQuery 的一个轻量级 Guid 字符串拓展插件. 轻量级Config文件AppSettings节点编辑帮助类
利用HttpWebRequest模拟表单提交

1 using System;
2 using System.Collections.Specialized;
3 using System.IO;
4 using System.Net;
5 using System.Text;
6
7 namespace Allyn.Common
8 {
9 public class HttpHelper
10 {
11 /// <summary>
12 /// 获取指定路径数据
13 /// </summary>
14 /// <param name="requestUri">提交路径</param>
15 /// <param name="cookie">Cookie容器对象</param>
16 /// <returns>字符串结果</returns>
17 public static string GetForm(string requestUri, CookieContainer cookie)
18 {
19 HttpWebRequest request = WebRequest.CreateHttp(requestUri);
20 request.Method = "get";
21 request.CookieContainer = cookie;
22 request.ContentLength = 0;
23
24 WebResponse response = request.GetResponse();
25 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
26 return reader.ReadToEnd();
27 }
28
29 /// <summary>
30 /// 默认表单提交
31 /// </summary>
32 /// <param name="requestUri">提交路径</param>
33 /// <param name="postData">提交数据</param>
34 /// <param name="cookie">Cookie容器对象</param>
35 /// <returns>字符串结果</returns>
36 public static string PostForm(string requestUri, NameValueCollection postData, CookieContainer cookie)
37 {
38 HttpWebRequest request = WebRequest.CreateHttp(requestUri);
39 request.Method = "post";
40 request.ContentType = "application/x-www-form-urlencoded";
41 request.CookieContainer = cookie;
42
43 StringBuilder stringBuilder = new StringBuilder();
44 foreach (string key in postData.Keys)
45 {
46 stringBuilder.AppendFormat("&{0}={1}", key, postData.Get(key));
47 }
48 byte[] buffer = Encoding.UTF8.GetBytes(stringBuilder.ToString().Trim('&'));
49 Stream requestStream = request.GetRequestStream();
50 requestStream.Write(buffer, 0, buffer.Length);
51 requestStream.Close();
52
53 WebResponse response = request.GetResponse();
54 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
55 return reader.ReadToEnd();
56 }
57
58 /// <summary>
59 /// 多部件表单提交
60 /// </summary>
61 /// <param name="requestUri">提交路径</param>
62 /// <param name="postData">提交数据.注:如果是文件路径,代表是文件.</param>
63 /// <param name="cookie">Cookie容器对象</param>
64 /// <returns>字符串结果</returns>
65 public static string PostFormMultipart(string requestUri, NameValueCollection postData, CookieContainer cookie)
66 {
67 string boundary = string.Format("-----{0}", DateTime.Now.Ticks.ToString("x"));
68 HttpWebRequest webrequest = WebRequest.CreateHttp(requestUri);
69 webrequest.CookieContainer = cookie;
70 webrequest.Timeout = 120000;
71 webrequest.Method = "post";
72 webrequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
73
74 Stream requestStream = webrequest.GetRequestStream();
75 foreach (string key in postData.Keys)
76 {
77 StringBuilder strBuilder = new StringBuilder();
78 strBuilder.AppendFormat("--{0}", boundary);
79 strBuilder.AppendFormat("\r\nContent-Disposition: form-data; name=\"{0}\"", key);
80 if (File.Exists(postData.Get(key)))
81 {
82 strBuilder.AppendFormat(";filename=\"{0}\"\r\nContent-Type: multipart/form-data\r\n\r\n", Path.GetFileName(postData.Get(key)));
83 byte[] buffer = Encoding.UTF8.GetBytes(strBuilder.ToString());
84 requestStream.Write(buffer, 0, buffer.Length);
85 //获取图片流
86 FileStream fileStream = new FileStream(postData.Get(key), FileMode.Open, FileAccess.Read);
87 BinaryReader binaryReader = new BinaryReader(fileStream);
88 byte[] fileBuffer = binaryReader.ReadBytes((int)fileStream.Length);
89 binaryReader.Close();
90 fileStream.Close();
91 requestStream.Write(fileBuffer, 0, fileBuffer.Length);
92 }
93 else
94 {
95 strBuilder.AppendFormat("\r\n\r\n{0}\r\n", postData.Get(key));
96 byte[] buff = Encoding.UTF8.GetBytes(strBuilder.ToString());
97 requestStream.Write(buff, 0, buff.Length);
98 }
99 }
100
101 byte[] boundaryBuffer = Encoding.UTF8.GetBytes(string.Format("\r\n--{0}\r\n", boundary));
102 requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
103 requestStream.Close();
104
105 WebResponse response = webrequest.GetResponse();
106 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
107 return reader.ReadToEnd();
108 }
109 }
110 }

JQuery 的一个轻量级 Guid 字符串拓展插件.

1 (function ($) {
2 function guid(g) {
3 var arr = new Array(); //存放32位数值的数组
4 if (typeof (g) == "string") { //如果构造函数的参数为字符串
5 initializeByString(arr, g);
6 } else {
7 initializeByOther(arr);
8 }
9 //返回一个值,该值指示 Guid 的两个实例是否表示同一个值。
10 this.equals = function (o) {
11 if (o && o.isGuid()) {
12 return this.toString() == o.toString();
13 }
14 else {
15 return false;
16 }
17 }
18 //Guid对象的标记
19 this.isGuid = function () {
20 return /^[0-9a-fA-F]{32}?$|^[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}?$/.test(this);
21 }
22 //返回 Guid 类的此实例值的 String 表示形式。
23 this.toString = function (format) {
24 if (typeof (format) == "string") {
25 if (format == "N" || format == "D" || format == "B" || format == "P") {
26 return toStringWithFormat(arr, format);
27 } else {
28 return toStringWithFormat(arr, "D");
29 }
30 }
31 else {
32 return toStringWithFormat(arr, "D");
33 }
34 }
35 //由字符串加载
36 function initializeByString(arr, g) {
37 g = g.replace(/\{|\(|\)|\}|-/g, "");
38 g = g.toLowerCase();
39 if (g.length != 32 || g.search(/[^0-9,a-f]/i) != -1) {
40 initializeByOther(arr);
41 } else {
42 for (var i = 0; i < g.length; i++) {
43 arr.push(g[i]);
44 }
45 }
46 }
47 //由其他类型加载
48 function initializeByOther(arr) {
49 var i = 32;
50 while (i--) {
51 arr.push("0");
52 }
53 }
54 /*
55 根据所提供的格式说明符,返回此 Guid 实例值的 String 表示形式。
56 N 32 位: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
57 D 由连字符分隔的 32 位数字 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
58 B 括在大括号中、由连字符分隔的 32 位数字:{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
59 P 括在圆括号中、由连字符分隔的 32 位数字:(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
60 */
61 function toStringWithFormat(arr, format) {
62 switch (format) {
63 case "N":
64 return arr.toString().replace(/,/g, "");
65 case "D":
66 var str = arr.slice(0, 8) + "-" + arr.slice(8, 12) + "-" + arr.slice(12, 16) + "-" + arr.slice(16, 20) + "-" + arr.slice(20, 32);
67 str = str.replace(/,/g, "");
68 return str;
69 case "B":
70 var str = toStringWithFormat(arr, "D");
71 str = "{" + str + "}";
72 return str;
73 case "P":
74 var str = toStringWithFormat(arr, "D");
75 str = "(" + str + ")";
76 return str;
77 default:
78 return new Guid();
79 }
80 }
81 }
82 $.extend({ guidEx: guid });
83 $.extend($.guidEx, {
84 enpty: function () {
85 return new guid();
86 },
87 newGuid: function () {
88 var g = "";
89 var i = 32;
90 while (i--) {
91 g += Math.floor(Math.random() * 16.0).toString(16);
92 }
93 return new guid(g);
94 },
95 isGuid: function (g) {
96 return /^[0-9a-fA-F]{32}?$|^[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}?$/.test(g);
97 }
98 });
99 })(jQuery);

轻量级Config文件AppSettings节点编辑帮助类

1 using System.Configuration;
2 using System.Windows.Forms;
3
4 namespace Allyn.Common
5 {
6 public class XmlHeper
7 {
8 ///<summary>
9 ///返回Config文件中appSettings配置节的value项
10 ///</summary>
11 ///<param name="strKey">节点Key</param>
12 ///<returns>值</returns>
13 public static string GetAppConfig(string strKey)
14 {
15 string file = Application.ExecutablePath;
16 Configuration config = ConfigurationManager.OpenExeConfiguration(file);
17
18 foreach (string key in config.AppSettings.Settings.AllKeys)
19 {
20 if (key == strKey)
21 {
22 return config.AppSettings.Settings[strKey].Value.ToString();
23 }
24 }
25 return string.Empty;
26 }
27
28 ///<summary>
29 ///在Config文件中appSettings配置节增加一对键值对
30 ///</summary>
31 ///<param name="newKey">节点名称</param>
32 ///<param name="newValue">信值</param>
33 public static void UpdateAppConfig(string newKey, string newValue)
34 {
35 string file = System.Windows.Forms.Application.ExecutablePath;
36 Configuration config = ConfigurationManager.OpenExeConfiguration(file);
37
38 bool exist = false;
39
40 foreach (string key in config.AppSettings.Settings.AllKeys)
41 {
42 if (key == newKey) { exist = true; }
43 }
44
45 if (exist) { config.AppSettings.Settings.Remove(newKey); }
46
47 config.AppSettings.Settings.Add(newKey, newValue);
48 config.Save(ConfigurationSaveMode.Modified);
49
50 ConfigurationManager.RefreshSection("appSettings");
51 }
52 }
53 }

利用HttpWebRequest模拟表单提交 JQuery 的一个轻量级 Guid 字符串拓展插件. 轻量级Config文件AppSettings节点编辑帮助类的更多相关文章
- 利用HttpWebRequest模拟表单提交
using System; using System.Collections.Specialized; using System.IO; using System.Net; using System. ...
- JQuery 的一个轻量级 Guid 字符串拓展插件.
(function ($) { function guid(g) { var arr = new Array(); //存放32位数值的数组 if (typeof (g) == "strin ...
- C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
其原理是,利用winfrom模拟表单提交数据.将要提交的參数提交给网页,网页运行代码.得到数据.然后Winform程序将网页的全部源码读取下来.这样就达到windows应用程序和web应用程序之间传參 ...
- 项目总结15:JavaScript模拟表单提交(实现window.location.href-POST提交数据效果)
JavaScript模拟表单提交(实现window.location.href-POST提交数据效果) 前沿 1-在具体项目开发中,用window.location.href方法下载文件,因windo ...
- 由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载,但是ajax实现的文件下载并不能触发浏览器的下载文件弹出框,这里通过模拟表单提交实现同样的效果。
由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载(这样的话ajax可以传递不同的参数),但是ajax实现的文 ...
- 表单提交---前端页面模拟表单提交(form)
有些时候我们的前端页面总没有<form></form>表单,但是具体的业务时,我们又必须用表单提交才能达到我们想要的结果,LZ最近做了一些关于导出的一些功能,需要调用浏览器默认 ...
- <记录> axios 模拟表单提交数据
ajax 可以通过 FormData 对象模拟表单提交数据 第一种方式:自定义FormData信息 //创建formData对象 var formData = new FormData(); //添加 ...
- HTTP通信模拟表单提交数据
前面记录过一篇关于http通信,发送数据的文章:http://www.cnblogs.com/hyyq/p/7089040.html,今天要记录的是如何通过http模拟表单提交数据. 一.通过GET请 ...
- 使用axios模拟表单提交
1.需求背景 最近在实验室写一个Spring前后端分离的项目,项目中使用Spring Security组件实现系统的认证和授权,当Security的认证模式设置为FormLogin时(如下代码),前端 ...
随机推荐
- 第八章 JVM性能监控与故障处理工具(2)
注意:该篇博客主要记录自<深入理解java虚拟机(第二版)> 说明:关于命令行的JVM性能监控与故障处理工具见<第七章 JVM性能监控与故障处理工具(1)> 1.图像化的故障处 ...
- C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数
在编程中经常需要用到数字与字符串的转换,下面就总结一下. 1.atoi() C/C++标准库函数,用于字符串到整数的转换. 函数原型:int atoi (const char * str); #inc ...
- Linux系统教程 标准输入/输出和重定向
1. 标准输入与输出 我们知道,执行一个shell命令行时通常会自动打开三个标准文件,即标准输入文件(stdin),通常对应终端的键盘:标准输出文件(stdout)和标准错误输出文件(stderr), ...
- 用VLC读取摄像头产生RTSP流,DSS主动取流转发(一)(二) 【转】
http://blog.csdn.net/fm0517/article/details/38110633 http://blog.csdn.net/fm0517/article/details/381 ...
- AS 常用插件 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Cognos值提示设置小技巧
针对值提示问题做一个小的总结: 1:显示类问题 如上图,如何让”英文参数名"和"分割线----"不显示,或者说指定中文显示值呢 (1):让”英文参数名"和&qu ...
- oauth2-server-php for windows 的那些坑 (研究中...)
oauth2-server-php for windows 的那些坑 在windwos 环境下,使用vs2017 for php 工具进行调试时,总是搞不出来, 于是分析了一下原因, 首先,oauth ...
- 【C#】利用JMail发送邮件
有用到需要发送帐号激活邮件,利用Jmail去做蛮简单的,先记录下: 1.首先到Jmail官网下载对应的版本,解压后安装(Jmail 4.4 免费版). 2.到安装目录就可以找到jmail.dll文件, ...
- 在 Ubuntu 12.04 上通过源码安装 Open vSwitch (OVS)
安装 Ubuntu 12.04, 而且更新系统 apt-getupdate; apt-getupgrade; 安装所需的package apt-get install automake autocon ...
- 【JavaScript】实现复选框的全选、全部不选、反选
以较为简洁的程序实现复选框的全选.全部不选.反选 操作. 并且将可变的部分设置为JS的参数,以实现代码复用. 全选和全不选 第一个参数为复选框名称,第二个参数为是全选还是全部不选. function ...