If a website has a form, for example (url:http://localhost/login.aspx):
Account
Password
We need to submit data to this form in the program, for this form, we can use the Webclient.uploaddata method to achieve, the data to be uploaded into a character, the program is simple:
String uristring = "Http://localhost/login.aspx";
Create a new WebClient instance.
WebClient mywebclient = new WebClient ();
String postdata = "Username=admin&password=admin";
Note the contenttype of this spelling string
MYWEBCLIENT.HEADERS.ADD ("Content-type", "application/x-www-form-urlencoded");
Convert into binary array
byte[] ByteArray = Encoding.ASCII.GetBytes (postdata);
Upload the data and get the binary data returned.
byte[] Responsearray = Mywebclient.uploaddata (uristring, "POST", ByteArray);
For a File Upload class form, for example (url:http://localhost/uploadfile.aspx):
File
For this form, we can use
String uristring = "http://localhost/uploadFile.aspx";
Create a new WebClient instance.
WebClient mywebclient = new WebClient ();
String fileName = @ "C:upload.txt";
Upload directly, and get the binary data returned.
byte[] Responsearray = Mywebclient.uploadfile (uristring, "POST", fileName);
There is also a form that has not only text, but also files, such as (url:http://localhost/uploaddata.aspx):
Filename
File
For this form, it seems that the previous two methods are not suitable for the first method, can not directly spell string, for the second, we can only transfer files, back to the first method, note the parameters:
Public byte[] Uploaddata (
String address,
String method,
Byte[] Data
);
In the first example, it is through the spelling string to get the byte[] Data parameter value, for this form is obviously not, in turn think, for uploaddata.aspx such a program, directly through the Web page submission data, background to get the flow is what? (In my previous blog, has analyzed this problem: ASP no component upload progress bar solution), the final data is as follows:
So as long as one such byte[] data post the past, you can achieve the same effect. However, it must be noted that for this file upload, its contenttype is not the same, such as the above, its contenttype as "MULTIPART/FORM-DATA;" boundary=---------------------------7d429871607fe ". With contenttype, we can know boundary (the "---------------------------7d429871607fe" above), knowing boundary we can construct the byte[we need. Data, and, finally, don't forget to pass our constructed ContentType to WebClient (for example: WebClient.Headers.Add ("Content-type", ContentType); You can upload file data through the Webclient.uploaddata method.
The specific code is as follows:
To generate a wrapper for a binary data class
Using System;
Using System.Web;
Using System.IO;
Using System.Net;
Using System.Text;
Using System.Collections;
Namespace Uploaddata.common
... {
/**////<summary>
To create a binary array for the Webclient.uploaddata method
</summary>
public class Createbytes
... {
Encoding Encoding = Encoding.UTF8;
/**////<summary>
Concatenation of all binary arrays into an array
</summary>
<param name= "bytearrays" > Array </param>
<returns></returns>
<remarks> Plus end Border </remarks>
Public byte[] Joinbytes (ArrayList bytearrays)
... {
int length = 0;
int readlength = 0;
Plus end boundary
String endboundary = boundary + "--rn"; End border
byte[] endboundarybytes = encoding. GetBytes (endboundary);
Bytearrays.add (endboundarybytes);
foreach (byte[] b in Bytearrays)
... {
length + = B.length;
}
byte[] bytes = new Byte[length];
Traverse replication
//
foreach (byte[] b in Bytearrays)
... {
B.copyto (bytes, readlength);
Readlength + = B.length;
}
return bytes;
}
public bool Uploaddata (string uploadurl, byte[] bytes, out byte[] responsebytes)
... {
WebClient WebClient = new WebClient ();
WEBCLIENT.HEADERS.ADD ("Content-type", ContentType);
This part needs to be perfected
String ContentType = "Application/octet-stream";
byte[] filebytes = new Byte[fs. Length];
Fs. Read (filebytes, 0, Convert.ToInt32 (FS). Length));
Generating binary arrays that need to be uploaded
Createbytes cb = new Createbytes ();
All form data
ArrayList Bytesarray = new ArrayList ();
General form
Bytesarray.add (CB. Createfielddata ("FileName", Txtfilename.text));
Bytesarray.add (CB. Createfielddata ("Amigotoken", Txtamigotoken.text));
File Form
Bytesarray.add (CB. Createfielddata ("Filedata", Path
, ContentType, filebytes));
Synthesize all forms and generate binary arrays
byte[] bytes = cb. Joinbytes (Bytesarray);
What is returned
Byte[] responsebytes;
Upload to specified URL
BOOL uploaded = cb. Uploaddata ("http://localhost/UploadData/UploadAvatar.aspx", Bytes, out responsebytes);
Output the returned content to a file
using (FileStream file = new FileStream (@ "C:response.text", FileMode.Create, FileAccess.Write, FileShare.Read))
... {
File. Write (responsebytes, 0, responsebytes.length);
}
The complete code is shown in attachment: Uploaddata.rar (38K) (http://bbs.openlab.net.cn/PostAttachment.aspx? postid=400927), after decompression to the web directory to build a virtual directory "Uploaddata", where uploadavatar.aspx is the actual upload processing page, if uploaded successfully, then return the file name and file type information. Default.aspx is the ASP.net page to invoke the Webclient.uploaddata method to submit the data, Uploaddatawin the project is the WinForm program call.
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.