Desktop applications can also provide HTTP file downloads

Source: Internet
Author: User
Tags header http request time limit

In the past, we certainly knew that when we were doing web apps, we are all likely to experience the ability to provide file downloads, such as a customer license management system I used to do, when customers purchase an ERP system, we usually generate a license file based on the machine code of the customer's machine, and the license file has a time limit. , a year later expires, after the expiration of the customer needs to obtain permission to express the continued use of our Super Cow B products, if the customer no longer obtain permission, it will indicate that the customer no longer use our Super Cow B products.

Later a thought, yes ah, rather than we get a Web program, let the customer login, input machine code, automatically generate the license file, and then the customer click on the page download on the line. Providing downloads is actually very common, like many software downloads.

However, have we ever considered that if we write an application that only provides a few features related to HTTP downloads, we don't seem to need to make a fuss about getting a server on a machine to get a Web site. Usually in this case, a small window program can be finished. So, the idea of using a desktop application to provide HTTP downloads is a thought.

In fact, this implementation is not complex, the System.Net namespace provides a HttpListener class, it can listen to the client incoming HTTP request, and then return a HttpListenerContext object, The HttpListenerContext object can then be used to process the request/response-related objects.

According to this idea, we can also easily provide the download function, the original and the Web Way is the same, is in response to the request to insert the Content-disposition header, the value of attachment;filename=< file name > method can be achieved.

Private async void Btnlisten_click (object sender, EventArgs e) {HttpListener listener = new HttpListener (); Listener.  
    Prefixes.add ("http://+:80/download/"); Listener.  
    Start ();  
    btnlisten.enabled = false; HttpListenerContext context = await listener.  
    Getcontextasync (); If (context!= null) {//Add Content-disposition header context. Response.appendheader ("Content-disposition", "attachment;filename=" + Webutility.urlencode (Path.GetFileName (  
        Lblfilepath.text)));  
                try {using (FileStream stream = File.openread (Lblfilepath.text)) { Add a content descriptor context.  
                Response.ContentType = MediaTypeNames.Application.Octet; Content length context. Response.contentlength64 = stream.  
                Length;  
                Postback data to the client byte[] buffer = new byte[1024]; int n = stream. Read (buffer, 0, buffer.)  
        Length);        while (n > 0) {context.  
                    Response.OutputStream.Write (buffer, 0, N); n = stream. Read (buffer, 0, buffer.)  
                Length); } context. Response.close (); Closes} catch (Exception ex) {MessageBox.Show Ex.  
        message); }} listener. Stop ();  
Stop listening btnlisten.enabled = true; }

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.