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. python之文件操作read

    #open函数,该函数用于文件处理,文件操作一共就有三种方法,打开文件#关闭文件, #先来说下打开文件,打开文件的模式有下面几种# 1.r,只读模式 f = open('test.log','r',e ...

  2. git设置别名alias

    每次用git拉去版本库都很烦,特别是要从非origin源,非master分支, 例如 git pull gitlab mybranch ,这样很蛋疼. 1.写个sh去处理 2.可以通过git的别名设置 ...

  3. Spring框架管理开源的连接池

    1. 管理DBCP连接池 * 先引入DBCP的2个jar包 * com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar * com.spring ...

  4. 7.25 js 自定义方法 a.b

    调用: $(function(){ Starinput.initiate({name:'qr1_z.startlevel',tar: $("#sitelogo1"), stars: ...

  5. 关于document.write(来自网络)

    对象属性: document.title                 //设置文档标题等价于HTML的<title>标签document.bgColor               / ...

  6. const当做标记的函数重载,但是仅仅是限于类里面的成员函数

    (1)我们知道函数的重载时根据函数的参数类型以及函数参数个数来重载的,不能用函数返回值来重载函数.但是有时候函数参数个数和函数参数类型重载函数会和默认参数发生冲突: int fun(int i,cha ...

  7. nodejs的优点

    nodejs主要用于搭建高性能的web服务器,优点如下: 可以解决高并发,它是单线程,当访问量很多时,将访问者分配到不同的内存中,不同的内存区做不同的事,以快速解决这个线程.就像医院的分科室看病人.效 ...

  8. angularjs写公共方法

    'use strict'; angular.module('fast-westone') .factory('commonUtilService', function () { return { /* ...

  9. 【转】Paxos算法3-实现探讨

    ——转自:{老码农的专栏} 前两篇Paxos算法的讨论,让我们对paxos算法的理论形成过程有了大概的了解,但距离其成为一个可执行的算法程序还有很长的路要走,原因是很多的细节和错误未被考虑.Googl ...

  10. MATLAB实现最优低通滤波器的函数

    MATLAB实现最优低通滤波器的函数 % Fs     --Data rate % Fpass  --pass band % Fstop  --Cutoff frequencies % Apass  ...