Example #1
0
        /// <summary>
        /// 上传文件到共享目录
        /// </summary>
        /// <param name="path">共享目录路径+文件名称</param>
        /// <param name="local">本地路径</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        static void ResponseWindowsShared(string path, string local, string username, string password)
        {
            System.Net.FileWebRequest request = null;
            System.IO.Stream          stream  = null;
            try
            {
                //时间戳
                string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
                Uri    uri         = new Uri(path);
                byte[] bytes       = System.IO.File.ReadAllBytes(local);
                request        = (System.Net.FileWebRequest)System.Net.FileWebRequest.Create(uri);
                request.Method = "POST";
                //设置获得响应的超时时间(300秒)
                request.Timeout       = 300000;
                request.ContentType   = "multipart/form-data; boundary=" + strBoundary;
                request.ContentLength = bytes.Length;

                System.Net.ICredentials ic = new System.Net.NetworkCredential(username, password);
                request.Credentials = ic;
                stream = request.GetRequestStream();
                stream.Seek(0, SeekOrigin.Begin);
                stream.Write(bytes, 0, bytes.Length);
                Console.WriteLine("上传成功!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }
Example #2
0
        /// <summary>
        /// 下载局域网文件
        /// </summary>
        /// <param name="path">局域网文件路径,如:\\192.168.10.1\app\app\123.zip</param>
        /// <param name="username">计算机名称</param>
        /// <param name="password">计算机密码</param>
        static void RequestWindowsShared(string path, string username, string password)
        {
            System.Net.FileWebRequest  request    = null;
            System.Net.FileWebResponse response   = null;
            System.IO.Stream           stream     = null;
            System.IO.FileStream       fileStream = null;
            try
            {
                Uri uri = new Uri(path);
                request = (System.Net.FileWebRequest)System.Net.FileWebRequest.Create(uri);
                System.Net.ICredentials ic = new System.Net.NetworkCredential(username, password);
                request.Credentials = ic;
                response            = (System.Net.FileWebResponse)request.GetResponse();
                stream = response.GetResponseStream();
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                string filename = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + System.IO.Path.GetFileName(path);
                fileStream = new FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
                fileStream.Write(bytes, 0, bytes.Length);
                fileStream.Flush();

                Console.WriteLine("下载成功!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                fileStream.Close();
                fileStream.Dispose();
                stream.Close();
                stream.Dispose();
            }
        }
 internal FileWebResponse(FileWebRequest request, Uri uri, FileAccess access, bool asyncHint)
 {
     try
     {
         this.m_fileAccess = access;
         if (access == FileAccess.Write)
         {
             this.m_stream = Stream.Null;
         }
         else
         {
             this.m_stream = new FileWebStream(request, uri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 0x2000, asyncHint);
             this.m_contentLength = this.m_stream.Length;
         }
         this.m_headers = new WebHeaderCollection(WebHeaderCollectionType.FileWebResponse);
         this.m_headers.AddInternal("Content-Length", this.m_contentLength.ToString(NumberFormatInfo.InvariantInfo));
         this.m_headers.AddInternal("Content-Type", "application/octet-stream");
         this.m_uri = uri;
     }
     catch (Exception exception)
     {
         Exception exception2 = new WebException(exception.Message, exception, WebExceptionStatus.ConnectFailure, null);
         throw exception2;
     }
 }
	protected virtual void Dispose(bool disposing)
	{
		Close();
		headers = null;
		request = null;
		contentLength = 0;
		disposed = true;
	}
	internal FileWebResponse(FileWebRequest request, Stream stream)
	{
		this.request = request;
		this.stream = stream;
		this.disposed = false;
		this.headers = new WebHeaderCollection();
		try
		{
			FileInfo fileInfo = new FileInfo(request.RequestUri.LocalPath);
			contentLength = fileInfo.Length;
			headers["Content-Length"] = contentLength.ToString();
		}
		catch(Exception e)
		{
		}
	}
		private static StreamFactoryResult OpenStreamIfModified(FileWebRequest request, LastModifyResult lastModifyResult)
		{
			var path = request.RequestUri.LocalPath;
			var fileInfo = new FileInfo(path);
			if (!fileInfo.Exists)
			{
				throw new ApplicationException(string.Format("File \"{0}\" does not exist", path));
			}
			return HasChanged(new DateTime(lastModifyResult.ModifyTimeUtcsTicks), fileInfo.LastWriteTimeUtc)
				? new StreamFactoryResult
				{
					Stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read),
					LastModifyResult = new LastModifyResult { ModifyTimeUtcsTicks = fileInfo.LastWriteTimeUtc.ToUniversalTime().Ticks }
				}
				: null;
		}
    // constructors

        internal FileWebResponse(FileWebRequest request, Uri uri, FileAccess access, bool asyncHint) {
            GlobalLog.Enter("FileWebResponse::FileWebResponse", "uri="+uri+", access="+access+", asyncHint="+asyncHint);
            try {
                m_fileAccess = access;
                if (access == FileAccess.Write) {
                    m_stream = Stream.Null;
                } else {

                    //
                    // apparently, specifying async when the stream will be read
                    // synchronously, or vice versa, can lead to a 10x perf hit.
                    // While we don't know how the app will read the stream, we
                    // use the hint from whether the app called BeginGetResponse
                    // or GetResponse to supply the async flag to the stream ctor
                    //

                    m_stream = new FileWebStream(request,
                                                 uri.LocalPath,
                                                 FileMode.Open,
                                                 FileAccess.Read,
                                                 FileShare.Read,
                                                 DefaultFileStreamBufferSize,
                                                 asyncHint
                                                 );
                    m_contentLength = m_stream.Length;
                }
                m_headers = new WebHeaderCollection(WebHeaderCollectionType.FileWebResponse);
                m_headers.AddInternal(HttpKnownHeaderNames.ContentLength, m_contentLength.ToString(NumberFormatInfo.InvariantInfo));
                m_headers.AddInternal(HttpKnownHeaderNames.ContentType, DefaultFileContentType);
                m_uri = uri;
            } catch (Exception e) {
                Exception ex = new WebException(e.Message, e, WebExceptionStatus.ConnectFailure, null);
                GlobalLog.LeaveException("FileWebResponse::FileWebResponse", ex);
                throw ex;
            }
            catch {
                Exception ex = new WebException(string.Empty, new Exception(SR.GetString(SR.net_nonClsCompliantException)), WebExceptionStatus.ConnectFailure, null);
                GlobalLog.LeaveException("FileWebResponse::FileWebResponse", ex);
                throw ex;
            }
            GlobalLog.Leave("FileWebResponse::FileWebResponse");
        }
        // constructors

        internal FileWebResponse(FileWebRequest request, Uri uri, FileAccess access, bool asyncHint)
        {
            GlobalLog.Enter("FileWebResponse::FileWebResponse", "uri=" + uri + ", access=" + access + ", asyncHint=" + asyncHint);
            try {
                m_fileAccess = access;
                if (access == FileAccess.Write)
                {
                    m_stream = Stream.Null;
                }
                else
                {
                    //
                    // apparently, specifying async when the stream will be read
                    // synchronously, or vice versa, can lead to a 10x perf hit.
                    // While we don't know how the app will read the stream, we
                    // use the hint from whether the app called BeginGetResponse
                    // or GetResponse to supply the async flag to the stream ctor
                    //

                    m_stream = new FileWebStream(request,
                                                 uri.LocalPath,
                                                 FileMode.Open,
                                                 FileAccess.Read,
                                                 FileShare.Read,
                                                 DefaultFileStreamBufferSize,
                                                 asyncHint
                                                 );
                    m_contentLength = m_stream.Length;
                }
                m_headers = new WebHeaderCollection(WebHeaderCollectionType.FileWebResponse);
                m_headers.AddInternal(HttpKnownHeaderNames.ContentLength, m_contentLength.ToString(NumberFormatInfo.InvariantInfo));
                m_headers.AddInternal(HttpKnownHeaderNames.ContentType, DefaultFileContentType);
                m_uri = uri;
            } catch (Exception e) {
                Exception ex = new WebException(e.Message, e, WebExceptionStatus.ConnectFailure, null);
                GlobalLog.LeaveException("FileWebResponse::FileWebResponse", ex);
                throw ex;
            }
            GlobalLog.Leave("FileWebResponse::FileWebResponse");
        }
        private static void GetRequestStreamCallback(object state)
        {
            LazyAsyncResult result      = (LazyAsyncResult)state;
            FileWebRequest  asyncObject = (FileWebRequest)result.AsyncObject;

            try
            {
                if (asyncObject.m_stream == null)
                {
                    asyncObject.m_stream     = new FileWebStream(asyncObject, asyncObject.m_uri.LocalPath, FileMode.Create, FileAccess.Write, FileShare.Read);
                    asyncObject.m_fileAccess = FileAccess.Write;
                    asyncObject.m_writing    = true;
                }
            }
            catch (Exception exception)
            {
                Exception exception2 = new WebException(exception.Message, exception);
                result.InvokeCallback(exception2);
                return;
            }
            result.InvokeCallback(asyncObject.m_stream);
        }
Example #10
0
 internal FileWebResponse(FileWebRequest request, Uri uri, FileAccess access, bool useAsync)
 {
     try
     {
         _fileAccess = access;
         if (access == FileAccess.Write)
         {
             _stream = Stream.Null;
         }
         else
         {
             _stream        = new WebFileStream(request, uri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultFileStreamBufferSize, useAsync);
             _contentLength = _stream.Length;
         }
         _headers = new WebHeaderCollection();
         _headers[HttpKnownHeaderNames.ContentLength] = _contentLength.ToString(NumberFormatInfo.InvariantInfo);
         _headers[HttpKnownHeaderNames.ContentType]   = DefaultFileContentType;
         _uri = uri;
     }
     catch (Exception e)
     {
         throw new WebException(e.Message, e, WebExceptionStatus.ConnectFailure, null);
     }
 }
Example #11
0
 internal FileWebResponse(FileWebRequest request, Uri uri, FileAccess access, bool useAsync)
 {
     try
     {
         _fileAccess = access;
         if (access == FileAccess.Write)
         {
             _stream = Stream.Null;
         }
         else
         {
             _stream = new WebFileStream(request, uri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultFileStreamBufferSize, useAsync);
             _contentLength = _stream.Length;
         }
         _headers = new WebHeaderCollection();
         _headers[HttpKnownHeaderNames.ContentLength] = _contentLength.ToString(NumberFormatInfo.InvariantInfo);
         _headers[HttpKnownHeaderNames.ContentType] = DefaultFileContentType;
         _uri = uri;
     }
     catch (Exception e)
     {
         throw new WebException(e.Message, e, WebExceptionStatus.ConnectFailure, null);
     }
 }
Example #12
0
 /// <summary>
 /// Initializes a new instance of the FileWebRequestAdapter class.
 /// </summary>
 /// <param name="fileWebRequest">The System.Net.WebRequest instance this object wraps.</param>
 public FileWebRequestAdapter(FileWebRequest fileWebRequest)
     : base(fileWebRequest)
 {
 }
Example #13
0
 internal FileWebStream(FileWebRequest webRequest, FileMode mode, FileAccess access, FileShare share)
     : base(webRequest.RequestUri.LocalPath, mode, access, share)
 {
     this.webRequest = webRequest;
 }
Example #14
0
 public FileWebStream(FileWebRequest request, string path, FileMode mode, FileAccess access, FileShare sharing, int length, bool async)
         : base(path, mode, access, sharing, length, async)
 {
     GlobalLog.Enter("FileWebStream::FileWebStream");
     m_request = request;
     GlobalLog.Leave("FileWebStream::FileWebStream");
 }
Example #15
0
			public override void Close() 
			{
				base.Close ();
				FileWebRequest req = webRequest;
				webRequest = null;
				if (req != null)
					req.Close ();
			}
Example #16
0
			internal FileWebStream (FileWebRequest webRequest,    
					   	FileMode mode,
					   	FileAccess access,
					   	FileShare share)
				: base (webRequest.RequestUri.LocalPath, 
					mode, access, share)
			{
				this.webRequest = webRequest;
			}
Example #17
0
 public WebFileStream(FileWebRequest request, string path, FileMode mode, FileAccess access, FileShare sharing, int length, bool async) :
     base(path, mode, access, sharing, length, async)
 {
     _request = request;
 }
Example #18
0
 public WebFileStream(FileWebRequest request, string path, FileMode mode, FileAccess access, FileShare sharing) :
     base(path, mode, access, sharing)
 {
     _request = request;
 }
Example #19
0
 public WebFileStream(FileWebRequest request, string path, FileMode mode, FileAccess access, FileShare sharing) :
     base(path, mode, access, sharing)
 {
     _request = request;
 }
Example #20
0
 public WebFileStream(FileWebRequest request, string path, FileMode mode, FileAccess access, FileShare sharing, int length, bool async) :
     base(path, mode, access, sharing, length, async)
 {
     _request = request;
 }
Example #21
0
 public override void Abort()
 {
     throw FileWebRequest.GetMustImplement();
 }