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模拟表单提交的更多相关文章

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

    利用HttpWebRequest模拟表单提交   1 using System; 2 using System.Collections.Specialized; 3 using System.IO; ...

  2. C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)

    其原理是,利用winfrom模拟表单提交数据.将要提交的參数提交给网页,网页运行代码.得到数据.然后Winform程序将网页的全部源码读取下来.这样就达到windows应用程序和web应用程序之间传參 ...

  3. 表单提交---前端页面模拟表单提交(form)

    有些时候我们的前端页面总没有<form></form>表单,但是具体的业务时,我们又必须用表单提交才能达到我们想要的结果,LZ最近做了一些关于导出的一些功能,需要调用浏览器默认 ...

  4. <记录> axios 模拟表单提交数据

    ajax 可以通过 FormData 对象模拟表单提交数据 第一种方式:自定义FormData信息 //创建formData对象 var formData = new FormData(); //添加 ...

  5. 项目总结15:JavaScript模拟表单提交(实现window.location.href-POST提交数据效果)

    JavaScript模拟表单提交(实现window.location.href-POST提交数据效果) 前沿 1-在具体项目开发中,用window.location.href方法下载文件,因windo ...

  6. 由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载,但是ajax实现的文件下载并不能触发浏览器的下载文件弹出框,这里通过模拟表单提交实现同样的效果。

    由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载(这样的话ajax可以传递不同的参数),但是ajax实现的文 ...

  7. HTTP通信模拟表单提交数据

    前面记录过一篇关于http通信,发送数据的文章:http://www.cnblogs.com/hyyq/p/7089040.html,今天要记录的是如何通过http模拟表单提交数据. 一.通过GET请 ...

  8. 使用axios模拟表单提交

    1.需求背景 最近在实验室写一个Spring前后端分离的项目,项目中使用Spring Security组件实现系统的认证和授权,当Security的认证模式设置为FormLogin时(如下代码),前端 ...

  9. c# 模拟表单提交,post form 上传文件、大数据内容

    表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipart/form-data,而且要设定一个 boundary 参数,这个参数是由应用程序自行产生,它会用来识别每 ...

随机推荐

  1. Island Transport

    Island Transport http://acm.hdu.edu.cn/showproblem.php?pid=4280 Time Limit: 20000/10000 MS (Java/Oth ...

  2. SVN服务器的安装和使用

    ------------------siwuxie095                                 SVN 服务器的安装         1.SVN 服务器,选择 VisualS ...

  3. QT隐式数据共享

    QT中许多C++类使用了隐式数据共享,最小化资源拷贝.当作为参数传递时,实际只传递了指针,这是底层完成的,程序员无需担心,即使是在多线程中,从Qt4开始: 记住,尽量使用const迭代器,vector ...

  4. python之面向对象之类变量和实例变量

    #Auther Bob #--*--conding:utf-8 --*-- #这里的变量全部都是实例变量 class Role(object): def __init__(self,name,role ...

  5. mysql 执行多线程临时方案

    sqr::IDatabase *db=NULL;IDbConnection *conn = NULL;int main(int argc, char* argv[]) { db = GetDataba ...

  6. html 5 如何限制上传的文件类型 (uploadifive)

    可以直接设置input标签的accept属性来限制上传文件的类型 <input type="file" accept="application/msword&quo ...

  7. 中介者模式(QQ聊天室我觉得是个很生动的例子简单易懂)

    设计模式之中介者模式(Mediator) 一.初识中介者模式 那些年,我们一起上过的大学,班级里有班长,有团书记.想一想如果没有QQ这种通讯工具的话,那么班长或者团支书该怎样下达消息呢??同时,班级上 ...

  8. 淘宝星星的思路c#

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  9. UUID含义及ubuntu配置系统默认JDK

    UUID含义是通用唯一识别码(Universally Unique Identifier) GUID是一个128位长的数字,一般用16进制表示.算法的核心思想是结合机器的网卡.当地时间.一个随即数来生 ...

  10. AJAX初尝试——ACM/ICPC类比赛气球管理系统

    很早之前做过一个,白板没界面,20秒暴力刷新,数据库每个team一个n列的对应n个题目的标记项,只能对单个比赛暴力把全部user_id导入单独的气球表(也就是cid=1000用这个表的话,cid100 ...