Write in front
Recently, I have been studying the document library of SharePoint, and in the process of uploading files to the document library, we need to simulate the post request and find several simulation methods, WebClient is a relatively simple way.
An example
Here is a simple ASPX page that accepts a POST request, with the following code:
1 namespaceWolfy.uploaddemo2 {3 Public Partial classDefault:System.Web.UI.Page4 {5 protected voidPage_Load (Objectsender, EventArgs e)6 {7 stringFileName = request.querystring["URL"];8 if(!string. IsNullOrEmpty (fileName))9 {TenStream st =Request.inputstream; One stringFilesavepath = Request.mappath ("~/upload/") +FileName; A byte[] buffer=New byte[St. Length]; -St. Read (Buffer,0, buffer. Length); - if(!file.exists (filesavepath)) the { - file.writeallbytes (filesavepath, buffer); - } - + } - } + } A}
Here, the querystring receives the URL parameter and accepts the file's data using the requested input stream.
Then, use WebClient to write a client that impersonates the request, with the following code:
1 namespaceWolfy.uploadexe2 {3 class Program4 {5 Static voidMain (string[] args)6 {7WebClient client =NewWebClient ();8Client. Querystring.add ("URL","1.png");Ten using(FileStream fs =NewFileStream ("1.png", FileMode.Open)) One { A byte[] buffer =New byte[FS. Length]; -Fs. Read (Buffer,0, buffer. Length); -Client. Uploaddata ("http://localhost:15887/Default.aspx", buffer); the } - - } - } +}
Debug state run ASPX, and then run EXE console program
If there is a verification message, you can add this sentence:
1 New NetworkCredential (" user name " " password " field ");
Summarize
Due to the current project, mobile app can not provide username and password, must use certificate to authenticate, found WebClient cannot support. The HttpWebRequest class is used for simulation. The use of it is below.
Uploading files to the server via WebClient analog post