Uploading files to the server via WebClient analog post

Source: Internet
Author: User

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

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.