Пример #1
0
 /// <summary>
 ///     Adds a named text value to the POST request.</summary>
 /// <param name="name">
 ///     The name of the value to add. For maximum compatibility with servers, use only printable ASCII characters in this
 ///     name. This field is encoded using UTF-8, which is supported by some modern servers, but not by others.</param>
 /// <param name="value">
 ///     The content to add as the value. Note that this is interpreted as Unicode text, and is a poor choice for binary
 ///     data. For binary data, see <see cref="AddFile(string,string,string)"/>.</param>
 public void AddField(string name, string value)
 {
     if (name == null)
     {
         throw new ArgumentNullException(nameof(name));
     }
     if (value == null)
     {
         throw new ArgumentNullException(nameof(value));
     }
     if (_request == null)
     {
         throw new InvalidOperationException("The request has already been sent in full. This operation is no longer legal.");
     }
     if (_currentFileStream != null)
     {
         _currentFileStream.Close();
         _currentFileStream = null;
     }
     stream.Write(_bytesBoundary);
     stream.Write("Content-Disposition: form-data; name=\"".ToUtf8());
     stream.Write(name.ToUtf8()); // there is no compatible way of doing this, so might as well just go the utf8-everywhere route...
     stream.Write("\"\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n".ToUtf8());
     stream.Write(value.ToUtf8());
     stream.Write(_bytesNewline);
 }
 /// <summary>
 ///     Finalizes the request and sends it to the remote host (or, if the request is not buffered, ensures the entire
 ///     request has been sent). Waits for response and returns the response object.</summary>
 public HttpWebResponse GetResponse()
 {
     if (_request == null)
         throw new InvalidOperationException("The request has already been sent in full. This operation is no longer legal.");
     if (_currentFileStream != null)
     {
         _currentFileStream.Close();
         _currentFileStream = null;
     }
     stream.Write(_bytesBoundaryLast);
     var result = (HttpWebResponse) _request.GetResponse();
     _request = null;
     return result;
 }
Пример #3
0
        /// <summary>
        ///     Finalizes the request and sends it to the remote host (or, if the request is not buffered, ensures the entire
        ///     request has been sent). Waits for response and returns the response object.</summary>
        public HttpWebResponse GetResponse()
        {
            if (_request == null)
            {
                throw new InvalidOperationException("The request has already been sent in full. This operation is no longer legal.");
            }
            if (_currentFileStream != null)
            {
                _currentFileStream.Close();
                _currentFileStream = null;
            }
            stream.Write(_bytesBoundaryLast);
            var result = (HttpWebResponse)_request.GetResponse();

            _request = null;
            return(result);
        }
Пример #4
0
 /// <summary>
 ///     Adds a named file (or, generally, a binary data field) to the POST request.</summary>
 /// <param name="name">
 ///     The name of the value to add. For maximum compatibility with servers, use only printable ASCII characters in this
 ///     name. This field is encoded using UTF-8, which is supported by some modern servers, but not by others.</param>
 /// <param name="filename">
 ///     The filename to use. The server may interpret this as it pleases, but this value will often end up being exposed
 ///     as the name of the uploaded file. For maximum compatibility with servers, use only printable ASCII characters in
 ///     this name. This field is encoded using UTF-8, which is supported by some modern servers, but not by
 ///     others.</param>
 /// <param name="contentType">
 ///     The content type to specify for this data/file. Some servers decide whether to accept or reject an upload based on
 ///     the content type. Specify <c>null</c> to prevent the inclusion of the Content-Type header.</param>
 /// <returns>
 ///     A stream into which the binary data is to be written. You may close this stream when done, but if you don't, it
 ///     will be closed automatically next time you add a field or a file, or if you call <see cref="GetResponse"/> (which
 ///     prevents further writing).</returns>
 public MultipartFileStream AddFile(string name, string filename, string contentType = "application/octet-stream")
 {
     if (name == null)
     {
         throw new ArgumentNullException(nameof(name));
     }
     if (filename == null)
     {
         throw new ArgumentNullException(nameof(filename));
     }
     if (_request == null)
     {
         throw new InvalidOperationException("The request has already been sent in full. This operation is no longer legal.");
     }
     if (_currentFileStream != null)
     {
         _currentFileStream.Close();
         _currentFileStream = null;
     }
     stream.Write(_bytesBoundary);
     stream.Write("Content-Disposition: form-data; name=\"".ToUtf8());
     stream.Write(name.ToUtf8());     // there is no compatible way of doing this, so might as well just go the utf8-everywhere route...
     stream.Write("\"; filename=\"".ToUtf8());
     stream.Write(filename.ToUtf8()); // same as above... See a related test here: http://greenbytes.de/tech/tc2231/#attwithutf8fnplain
     if (contentType == null)
     {
         stream.Write("\"\r\n\r\n".ToUtf8());
     }
     else
     {
         stream.Write("\"\r\nContent-Type: ".ToUtf8());
         stream.Write(contentType.ToUtf8());
         stream.Write("\r\n\r\n".ToUtf8());
     }
     _currentFileStream = new MultipartFileStream(stream);
     return(_currentFileStream);
 }
 /// <summary>
 ///     Adds a named text value to the POST request.</summary>
 /// <param name="name">
 ///     The name of the value to add. For maximum compatibility with servers, use only printable ASCII characters in this
 ///     name. This field is encoded using UTF-8, which is supported by some modern servers, but not by others.</param>
 /// <param name="value">
 ///     The content to add as the value. Note that this is interpreted as Unicode text, and is a poor choice for binary
 ///     data. For binary data, see <see cref="AddFile(string,string,string)"/>.</param>
 public void AddField(string name, string value)
 {
     if (name == null)
         throw new ArgumentNullException("name");
     if (value == null)
         throw new ArgumentNullException("value");
     if (_request == null)
         throw new InvalidOperationException("The request has already been sent in full. This operation is no longer legal.");
     if (_currentFileStream != null)
     {
         _currentFileStream.Close();
         _currentFileStream = null;
     }
     stream.Write(_bytesBoundary);
     stream.Write("Content-Disposition: form-data; name=\"".ToUtf8());
     stream.Write(name.ToUtf8()); // there is no compatible way of doing this, so might as well just go the utf8-everywhere route...
     stream.Write("\"\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n".ToUtf8());
     stream.Write(value.ToUtf8());
     stream.Write(_bytesNewline);
 }
 /// <summary>
 ///     Adds a named file (or, generally, a binary data field) to the POST request.</summary>
 /// <param name="name">
 ///     The name of the value to add. For maximum compatibility with servers, use only printable ASCII characters in this
 ///     name. This field is encoded using UTF-8, which is supported by some modern servers, but not by others.</param>
 /// <param name="filename">
 ///     The filename to use. The server may interpret this as it pleases, but this value will often end up being exposed
 ///     as the name of the uploaded file. For maximum compatibility with servers, use only printable ASCII characters in
 ///     this name. This field is encoded using UTF-8, which is supported by some modern servers, but not by
 ///     others.</param>
 /// <param name="contentType">
 ///     The content type to specify for this data/file. Some servers decide whether to accept or reject an upload based on
 ///     the content type. Specify <c>null</c> to prevent the inclusion of the Content-Type header.</param>
 /// <returns>
 ///     A stream into which the binary data is to be written. You may close this stream when done, but if you don't, it
 ///     will be closed automatically next time you add a field or a file, or if you call <see cref="GetResponse"/> (which
 ///     prevents further writing).</returns>
 public MultipartFileStream AddFile(string name, string filename, string contentType = "application/octet-stream")
 {
     if (name == null)
         throw new ArgumentNullException("name");
     if (filename == null)
         throw new ArgumentNullException("filename");
     if (_request == null)
         throw new InvalidOperationException("The request has already been sent in full. This operation is no longer legal.");
     if (_currentFileStream != null)
     {
         _currentFileStream.Close();
         _currentFileStream = null;
     }
     stream.Write(_bytesBoundary);
     stream.Write("Content-Disposition: form-data; name=\"".ToUtf8());
     stream.Write(name.ToUtf8()); // there is no compatible way of doing this, so might as well just go the utf8-everywhere route...
     stream.Write("\"; filename=\"".ToUtf8());
     stream.Write(filename.ToUtf8()); // same as above... See a related test here: http://greenbytes.de/tech/tc2231/#attwithutf8fnplain
     if (contentType == null)
         stream.Write("\"\r\n\r\n".ToUtf8());
     else
     {
         stream.Write("\"\r\nContent-Type: ".ToUtf8());
         stream.Write(contentType.ToUtf8());
         stream.Write("\r\n\r\n".ToUtf8());
     }
     _currentFileStream = new MultipartFileStream(stream);
     return _currentFileStream;
 }