发请求

public string CameraFileUpload(string url,string path,string serverPath,string uploadfileName)
{ System.Net.ServicePointManager.DefaultConnectionLimit = ;
System.GC.Collect(); HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.KeepAlive = false;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); int pos = path.LastIndexOf("\\");
string fileName = path.Substring(pos + ); //请求头部信息
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); request.Headers.Add("serverPath", serverPath);
request.Headers.Add("uploadfileName", uploadfileName); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] bArr = new byte[fs.Length];
fs.Read(bArr, , bArr.Length);
fs.Close(); Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, , itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes, , postHeaderBytes.Length);
postStream.Write(bArr, , bArr.Length);
postStream.Write(endBoundaryBytes, , endBoundaryBytes.Length);
postStream.Close(); //发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd(); if (request != null)
{
request.Abort();
request = null;
}
if (response != null)
{
response.Close();
response = null;
} return content;
}

处理请求:

 protected void Page_Load(object sender, EventArgs e)
{
try
{
HttpPostedFile file = Request.Files[]; string temp = Request.Form["temp"];
string serverPath = Request.Headers["serverPath"];
string uploadfileName = Request.Headers["uploadfileName"]; filePathCheck(serverPath);
file.SaveAs(Server.MapPath(serverPath + "\\" + uploadfileName));
Response.Write("上传成功");
}
catch (Exception ex)
{
Response.Write("上传失败 原因:" + ex.Message);
}
} public void filePathCheck(string path)
{
if (!Directory.Exists(Server.MapPath(path)))
Directory.CreateDirectory(Server.MapPath(path));
}

模拟form表单请求上传文件的更多相关文章

  1. JAVA入门[16]-form表单,上传文件

    一.如何传递参数 使用 @RequestParam 可以传递查询参数.例如:http://localhost:8092/category/detail?id=1 @RequestMapping(&qu ...

  2. Java模拟表单POST上传文件

    JAVA模拟表单POST上传文件 import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.io ...

  3. Linux curl 模拟form表单提交信息和文件

    Linux curl 模拟form表单提交信息和文件   curl是一个命令行方式下传输数据的开源传输工具,支持多种协议:FTP.HTTP.HTTPS.IMAP.POP3.TELNET等,功能超级强大 ...

  4. Django---CBV和FBV的使用,CBV的流程,给视图加装饰器,Request对象方法,属性和Response对象,form表单的上传

    Django---CBV和FBV的使用,CBV的流程,给视图加装饰器,Request请求对象方法,属性和Response响应对象,form表单的上传 一丶CBV和FBV       在Django中存 ...

  5. js_ajax模拟form表单提交_多文件上传_支持单个删除

    需求场景: 用一个input type="file"按钮上传多张图片,可多次上传,可单独删除,最后使用ajax模拟form表单提交功能提交到指定方法中: 问题:由于只有一个file ...

  6. 【温故知新】Java web 开发(三)Form表单与上传下载文件

    简介:在一和二的基础之上,这次来记录下如何在页面提交表单数据,以及文件的上传和下载整个流程,请求也不仅限于GET了,也有POST了. 1. 为了方便,在 webapp 下直接新建一个 index.ht ...

  7. 通过C#的HttpClient模拟form表单请求

    post提交表单一般无非是一般text文本和文件类型,如下 <input type="file"/> <input type="text"/& ...

  8. html-4, form 表单 输入、传文件、单选、多选、下拉菜单、文本描述、重置、submit、按钮限制输入

    <!-- form HTTP协议 action:提交的服务器网址 method:get(默认)| post(应用:登录注册.上传文件) 页面中的a img link 默认是get请求 input ...

  9. web 表单方式上传文件方法(不用flash插件)

    原理:使用表单的input type="file"标签,通过ajax提交表单请求,后台获取请求中的文件信息,进行文件保存操作 由于我测试用的做了一个上传文件和上传图片方法,所以我有 ...

随机推荐

  1. sql中同一个Trigger里同时包含Insert,Update,Delete

    sql中同一个Trigger里同时包含Insert,Update,Delete SQLServer是靠Inserted表和Deleted表来处理的,判断一下就可以了,只不过比ORACLE麻烦一点 cr ...

  2. win10 显示详细信息窗格

      win10 显示详细信息窗格 CreateTime--2018年5月26日09点13分 Author:Marydon 1.说明: win10无法像win7那样将详细信息窗格显示在窗口的底部,只能显 ...

  3. zend studio 10安装+破解+汉化

    http://pan.baidu.com/share/link?shareid=1857675714&uk=3325301372 在线安装汉化包:http://309614533.blog.1 ...

  4. iframe双滚动栏 解决方案 CSS3 overflow-y 属性

     裁剪 div 元素中内容的左/右边缘 - 假设溢出元素的内容区域的话: div { overflow-y:hidden; } <!DOCTYPE html> <html> ...

  5. UE初写小项目问题之命令行报错:Expected indentation of 4 spaces but found 6

    一.问题 刚开始用一个小项目练手,用vue-cli初始化项目,直接安装了所有的东西,如vue-router.ESLint.unit tests等等,当时只懂得vue-router是什么. 问题来了,在 ...

  6. centos中安装openjdk

    openjdk的java环境还是简单好用,至于其他的,现在又用不到,为啥要费事安装它们? 步骤: 1.yum search java | grep jdk [root@noi ~]# yum sear ...

  7. Rigidbody-ClosestPointOnBounds测试

    可见是Collider的Bounds

  8. Spring学习10-SpringMV核心组件2及SpringMVC项目示例

    一.SpringMVC核心接口     5.ViewResolver接口--视图解析接口 继承体系:      5.View接口--视图接口   二.SpringMVC项目示例   (1)建立动态we ...

  9. vuex中store分文件时候index.js进行文件整合

    import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); import getters from './getters.js' impo ...

  10. shiro身份认证

    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...