zoukankan      html  css  js  c++  java
  • 基于HTTP的上载和下载

    基于http流上载:
    ---------------------
    private void cmdSendFileData_Click(object sender, System.EventArgs e)
    {
    // This method takes a local file named datafile.txt and sends its
    // contents via a WebRequest to a website. The contents are sent via
    // HTTP using the Request stream of the WebRequest object. The file
    // is accessed using a FileStream.
    string strMsg = "In order to run this part of the sample, you must adjust the security settings" +
    " for the physical directory that contains the ASPX files." + Environment.NewLine + Environment.NewLine +
    "Please see the Readthis.htm file for more information." + Environment.NewLine +
    "Also see the source code (for the procedure cmdSendFileData_Click) to remove this warning after the security settings have been adjusted.";

    FileStream fs = null; //' To access the local file;
    WebRequest req = null; //' Reference to the Webrequest;
    // Wrap the stream access in a try {/Finally block to guarantee a timely
    // release of the stream resources.

    try
    {
    // Access the file
    fs = new FileStream("..//..//DataFile.txt", FileMode.Open);
    // Create the WebRequest instance
    req = WebRequest.Create("http://localhost/SendAndReceiveDataWebPages/SendData.aspx");
    // Use POST since we're sending content in the body.
    req.Method = "POST";
    // Copy from the file into the RequestStream
    CopyData(fs, req.GetRequestStream());
    }
    catch( Exception exp)
    {
    MessageBox.Show(exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
    }
    finally
    {
    try
    {
    // Guarantee the streams will be closed
    if (req != null) req.GetRequestStream().Close();
    if (fs != null) fs.Close();
    }
    catch
    {
    // Eat the error if we get one
    }
    }

    WebResponse rsp = null;

    try
    {
    // This actually sends the data to the Web Server
    rsp = req.GetResponse();
    if (Convert.ToDouble(rsp.Headers["Content-Length"]) == 0)
    {
    MessageBox.Show("Data Sent Sucessfully.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    }
    catch( Exception exp)
    {
    MessageBox.Show(exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
    try
    {
    if ( rsp != null) rsp.Close();
    }
    catch{
    }
    }
    }   
        
        基于http流下载:
    ---------------------

    private void cmdReceiveDataFile_Click(object sender, System.EventArgs e)
    {
    // This method requests and receives an XML file from a website.
    // The file is streamed back to this method, which copies the contents
    // into a local file named ReceivedXMLFile.xml. The contents are streamed
    // using the ResponseStream of the WebResponse class. The stream access
    // is wrapped in try {/catch blocks to ensure timely release of the
    // resources.
    string strMsg = "In order to run this part of the sample, you must adjust the security settings" +
    " for the physical directory that contains the ASPX files." + Environment.NewLine + Environment.NewLine +
    "Please see the Readthis.htm file for more information." + Environment.NewLine +
    "Also see the source code (for the procedure cmdReceiveImageFile_Click) to remove this warning after the security settings have been adjusted.";

    FileStream fs = null; //' To access the local file;
    WebRequest req;
    StreamReader sr;

    try
    {
    // This sets up the Request instance
    req = WebRequest.Create("http://localhost/SendAndReceiveDataWebPages/ReceiveData.aspx");
    // Use a GET since no data is being sent to the web server
    req.Method = "GET";
    // This causes the round-trip
    WebResponse rsp = req.GetResponse();
    try
    {
    // Open the file to stream in the content
    fs = new FileStream("ReceivedFile.rar", FileMode.Create);
    // Copy the content from the response stream to the file.
    CopyData(rsp.GetResponseStream(), fs);
    }
    catch( Exception exp)
    {
    // Will catch any error that we're not explicitly trapping.
    MessageBox.Show(exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
    }
    finally
    {
    // Guarantee the streams will be closed
    if (rsp != null) rsp.GetResponseStream().Close();
    if (fs != null) fs.Close();
    }
    MessageBox.Show("Receive of data file completed successfully!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch( Exception exp)
    {
    // Will catch any error that we're not explicitly trapping.
    MessageBox.Show(exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
    }
    }

    --------------------------
    方法:
    --------------------------
    private void CopyData(Stream FromStream, Stream ToStream)
    {
    // This routine copies content from one stream to another, regardless
    // of the media represented by the stream.
    // This will track the # bytes read from the FromStream
    int intBytesRead;
    // The maximum size of each read
    const int intSize = 4096;
    Byte[] bytes = new Byte[intSize];
    // Read the first bit of content, then write and read all the content
    // From the FromStream to the ToStream.
    intBytesRead = FromStream.Read(bytes, 0, intSize);

    while (intBytesRead > 0)
    {
    ToStream.Write(bytes, 0, intBytesRead);
    intBytesRead = FromStream.Read(bytes, 0, intSize);
    }

    }
  • 相关阅读:
    Android Shell命令dumpsys
    查看Android内存的8中方法
    Viewpager 的相关总结
    FileZilla等软件搭建ftp服务器
    Linux系统下邮件服务器的搭建(Postfix+Dovecot)
    PHP 打乱数组
    PHP 日期 加减 月数,天数,周数,小时,分,秒等等
    PHP 常用函数
    111
    phpStudy启动失败提示:缺少VC9运行库
  • 原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100707.html
Copyright © 2011-2022 走看看