利用HttpWebRequest模拟表单提交
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text; namespace Allyn.Common
{
public class HttpHelper
{
/// <summary>
/// 获取指定路径数据
/// </summary>
/// <param name="requestUri">提交路径</param>
/// <param name="cookie">Cookie容器对象</param>
/// <returns>字符串结果</returns>
public static string GetForm(string requestUri, CookieContainer cookie)
{
HttpWebRequest request = WebRequest.CreateHttp(requestUri);
request.Method = "get";
request.CookieContainer = cookie;
request.ContentLength = ; WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
} /// <summary>
/// 默认表单提交
/// </summary>
/// <param name="requestUri">提交路径</param>
/// <param name="postData">提交数据</param>
/// <param name="cookie">Cookie容器对象</param>
/// <returns>字符串结果</returns>
public static string PostForm(string requestUri, NameValueCollection postData, CookieContainer cookie)
{
HttpWebRequest request = WebRequest.CreateHttp(requestUri);
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookie; StringBuilder stringBuilder = new StringBuilder();
foreach (string key in postData.Keys)
{
stringBuilder.AppendFormat("&{0}={1}", key, postData.Get(key));
}
byte[] buffer = Encoding.UTF8.GetBytes(stringBuilder.ToString().Trim('&'));
Stream requestStream = request.GetRequestStream();
requestStream.Write(buffer, , buffer.Length);
requestStream.Close(); WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
} /// <summary>
/// 多部件表单提交
/// </summary>
/// <param name="requestUri">提交路径</param>
/// <param name="postData">提交数据.注:如果是文件路径,代表是文件.</param>
/// <param name="cookie">Cookie容器对象</param>
/// <returns>字符串结果</returns>
public static string PostFormMultipart(string requestUri, NameValueCollection postData, CookieContainer cookie)
{
string boundary = string.Format("-----{0}", DateTime.Now.Ticks.ToString("x"));
HttpWebRequest webrequest = WebRequest.CreateHttp(requestUri);
webrequest.CookieContainer = cookie;
webrequest.Timeout = ;
webrequest.Method = "post";
webrequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); Stream requestStream = webrequest.GetRequestStream();
foreach (string key in postData.Keys)
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.AppendFormat("--{0}", boundary);
strBuilder.AppendFormat("\r\nContent-Disposition: form-data; name=\"{0}\"", key);
if (File.Exists(postData.Get(key)))
{
strBuilder.AppendFormat(";filename=\"{0}\"\r\nContent-Type: multipart/form-data\r\n\r\n", Path.GetFileName(postData.Get(key)));
byte[] buffer = Encoding.UTF8.GetBytes(strBuilder.ToString());
requestStream.Write(buffer, , buffer.Length);
//获取图片流
FileStream fileStream = new FileStream(postData.Get(key), FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
byte[] fileBuffer = binaryReader.ReadBytes((int)fileStream.Length);
binaryReader.Close();
fileStream.Close();
requestStream.Write(fileBuffer, , fileBuffer.Length);
}
else
{
strBuilder.AppendFormat("\r\n\r\n{0}\r\n", postData.Get(key));
byte[] buff = Encoding.UTF8.GetBytes(strBuilder.ToString());
requestStream.Write(buff, , buff.Length);
}
} byte[] boundaryBuffer = Encoding.UTF8.GetBytes(string.Format("\r\n--{0}\r\n", boundary));
requestStream.Write(boundaryBuffer, , boundaryBuffer.Length);
requestStream.Close(); WebResponse response = webrequest.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
}
}
}
利用HttpWebRequest模拟表单提交的更多相关文章
- 利用HttpWebRequest模拟表单提交 JQuery 的一个轻量级 Guid 字符串拓展插件. 轻量级Config文件AppSettings节点编辑帮助类
利用HttpWebRequest模拟表单提交 1 using System; 2 using System.Collections.Specialized; 3 using System.IO; ...
- C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
其原理是,利用winfrom模拟表单提交数据.将要提交的參数提交给网页,网页运行代码.得到数据.然后Winform程序将网页的全部源码读取下来.这样就达到windows应用程序和web应用程序之间传參 ...
- 表单提交---前端页面模拟表单提交(form)
有些时候我们的前端页面总没有<form></form>表单,但是具体的业务时,我们又必须用表单提交才能达到我们想要的结果,LZ最近做了一些关于导出的一些功能,需要调用浏览器默认 ...
- <记录> axios 模拟表单提交数据
ajax 可以通过 FormData 对象模拟表单提交数据 第一种方式:自定义FormData信息 //创建formData对象 var formData = new FormData(); //添加 ...
- 项目总结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实现的文 ...
- HTTP通信模拟表单提交数据
前面记录过一篇关于http通信,发送数据的文章:http://www.cnblogs.com/hyyq/p/7089040.html,今天要记录的是如何通过http模拟表单提交数据. 一.通过GET请 ...
- 使用axios模拟表单提交
1.需求背景 最近在实验室写一个Spring前后端分离的项目,项目中使用Spring Security组件实现系统的认证和授权,当Security的认证模式设置为FormLogin时(如下代码),前端 ...
- c# 模拟表单提交,post form 上传文件、大数据内容
表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipart/form-data,而且要设定一个 boundary 参数,这个参数是由应用程序自行产生,它会用来识别每 ...
随机推荐
- INI
.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理 ...
- Python requests 使用心得
最近在用requests写一些项目,遇见了一些问题,百度了很多,有些都不太好使,最后看了下requestsAPI文档,才明白了很多,最后项目趋于稳定.看来学东西还是API文档比较权威啊~ 问题场景 项 ...
- cin 不能直接读入空格,可以用getline(PAT统计字符数)
#include <iostream>#include <cstring>using namespace std; int main(){ string str; ...
- win下php5.4安装ffmpeg-php扩展
1.ffmpeg的官网没有提供ffmpeg-php dll的扩展下载. http://ffmpeg-php.sourceforge.net/ 虽然在http://sourceforge.net/上提供 ...
- maven项目工程目录约定
使用maven创建的工程我们称它为maven工程,maven工程具有一定的目录规范,如下: src/main/java —— 存放项目的.java文件 src/main/resources —— 存放 ...
- Html使用Iframe无刷新上传文件,后台接收
html代码:我是发送请求到teacher_center.aspx,不是到.ashx一般处理程序里,需要加 runat="server",有空我再试试发送请求到 .ashx 里 & ...
- 不要用for in语句对数组进行遍历
for...in主要用于对数组和对象的属性进行遍历.for ... in 循环中的代码每执行一次,就会对数组的元素或者对象的属性进行一次操作. 语法:for (variable in object) ...
- NOIP 2016 游记
- dateframe取数据
import numpy as npimport pandas as pd## x1=[1,2,3,4]# x2=[4,5,6,7]# x3=[7,8,9,10]# df=pd.DataFrame(# ...
- bitnami redmine svn配置
采用bitnami 方案安装redmine svn服务器端会自己进行安装 1.创建版本库 首先进入remine安装目录的subversion/bin目录,例如我的安装目录是“/opt/redmine/ ...