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. Ubuntu安装libevent

    背景: 版本: libevent 2.1.6beta linux下: 按照github官方做法: $ sudo apt-get install openssl $ mkdir build && ...

  2. google中guava类库:AsyncEventBus

    1.guava事件总线(AsyncEventBus)使用 1.1引入依赖 <dependency> <groupId>com.google.guava</groupId& ...

  3. catkin-tools

    http://catkin-tools.readthedocs.io/en/latest/cheat_sheet.html 一.Initializing Workspaces初始化工作空间 初始化具有 ...

  4. _GET_

    _GET_:读取属性,没有自动创建

  5. python while语句写法

    count=5 while count>0: print 'i love python' count=count-1 else: print 'over'

  6. 通过ANT实现jmeter批量执行脚本、生成报告、发送邮件全套build.xml文件

    在开始通过ANT运行build.xml之前,有一步必须要做,那就是将JMeter所在目录下extras子目录里的ant-JMeter-1.1.1.jar复制到Ant所在目录lib子目录之下,这样Ant ...

  7. 并发编程(三)Promise, Future 和 Callback

    并发编程(三)Promise, Future 和 Callback 异步操作的有两个经典接口:Future 和 Promise,其中的 Future 表示一个可能还没有实际完成的异步任务的结果,针对这 ...

  8. apk签名方法

    生成签名文件: 1.右击项目管理器 选择 Export...  菜单: 2.在弹出的Export窗口中选择 Android->Export Android Application 后 next: ...

  9. 2018.09.15 poj2117Electricity(割点)

    传送门 其实求一个图删除一个点之后,联通块最多有多少. 直接tarjan求割点更新答案就行了. 但注意原图不一定连通. 代码: #include<iostream> #include< ...

  10. Django 必会面试题总结

    1 列举Http请求中常见的请求方式 HTTP请求的方法: HTTP/1.1协议中共定义了八种方法(有时也叫“动作”),来表明Request-URL指定的资源不同的操作方式   注意: 1)方法名称是 ...