Implement the use of HTTP to send multipart/form-data HTTP forms

Source: Internet
Author: User

. Net FrameWork encapsulates the HTTP protocol, which makes it very simple to use the HTTP protocol,. net can only use application/x-www-form-urlencoded to POST data, using System. net. the file passing (multipart/form-data mode) on the WebClient can only upload a single file. In practice, there are many requirements for hybrid submission of files and forms, and multi-File Upload. These are the current requirements.. net framework is not supported. These requirements all send data in the form of multipart/form-data, that is, currently. net does not support multipart/form-data form submission. The Source Code listed in this article is to solve this problem.

The multipart/form-data HTTP protocol is roughly as follows, and red is an additional one:

POST/msb/WebForm1.aspx HTTP/1.1
Connection: Keep-Alive
Content-Length: 458
Content-Type: multipart/form-data; boundary = ----------------------------- 7d6bb34502ce [upload mode, boundary]
Accept :*/*
Secondary CT: 100-continue
Host: localhost
Referer: http://mtv.1ting.com/
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;. net clr 1.1.4322;. net clr 2.0.50727)

------------------------------- 7d6bb34502ce [the boundary starts with 2 more hyphens]
Content-Disposition: form-data; name = "key1 [form domain name]"

Ffffffffffffffffffffffffffffffffffffffffffffffffffffffff [form field value]
------------------------------- 7d6bb34502ce [the boundary starts with 2 more hyphens]
Content-Disposition: form-data; name = "file [file type form domain name]"; filename = "d: \ 1.rar[ file name]"
Content-Type: application/x-msdownload [file Type]

[File content]
----------------------------- 7d6bb34502ce -- [the last boundary, with two hyphens at the beginning and end]

Here is the source code: http://blog.csdn.net/images/blog_csdn_net/Qqwwee_Com/HttpProc.gif

Call code:

Dim a As New ArrayList
A. Add (New EntityFormValue ("key1", "key1") 'form
A. Add (New EntityFormValue ("key2", "value2") 'form
A. Add (New EntityFormFile ("file", "d: \ 1.rar") 'file
Dim http As New HttpProc ("http: // localhost/msb/WebForm1.aspx ")
Console. WriteLine (http. PostMultipartForm ())

About HttpProc

Http is in. net frameWork supports an extended Class Based on the http protocol, which is used to simplify http GET and POST operations and can meet most of the daily HTTP processing requirements, this is reflected in all versions of my csdn assistant.

The following is a simulated login. The code is very concise:

 

Dim Url As String = "http: // localhost/login. aspx" 'address
Dim postDate As String = "username = user123 & passWork = 123456"
'User name and password
Dim Res As String = ""
Dim http As New HttpProc (Url)
Res = http. Proc 'to get a response

Original article address: http://blog.csdn.net/Qqwwee_Com/archive/2006/04/08/655711.aspx (relay please)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.