如何设置system.io.stream对象以写入值 [英] How to sets the system.io.stream object for write a value
问题描述
我已经尝试了一天左右但是无法正常工作!
我想设置一个System.IO.Stream对象进一步使用!喜欢WebRequest.GetRequestStream()
任何帮助都将不胜感激!
我有什么试过:
使用System;
使用System.Collections.Generic;
使用System .Collections.Specialized;
使用System.Text;
使用System.IO;
namespace Utility
{
内部类HttpRequest
{
private string url;
public byte [] header_bytes;
protected const string crLf =\\\\ n;
私有版_version = HttpVersion.Version11;
public NameValueCollection header
{
get;
set;
}
公共字符串方法
{br / >
get;
set;
}
public Version version
{
get {return _version; }
set {_version = value; }
}
公共HttpRequest(字符串网址)
{
this .url = url;
headers = new NameValueCollection();
}
public static HttpRequest create( Uri uri)
{
var req = new HttpRequest(uri.PathAndQuery);
req.headers [Host] = uri。 DnsSafeHost;
返回要求;
}
公共流get_stream( )
{
get_header_bytes();
返回新的MemoryStream(header_bytes);
}
public HttpResponse get_response(Stream stream)
{
if(header_bytes == null)get_header_bytes();
stream.Write(header_bytes,0,header_bytes.Length);
返回HttpResponse.read(流);
}
private void get_header_bytes()
{
var header = new StringBuilder();
header.AppendFormat({0} {1} HTTP / {2} {3},方法,网址,版本,crLf);
foreach(header.AllKeys中的var键)
header.AppendFormat({0} :{1} {2},key,headers [key],crLf);
header.AppendLine();
header_bytes = Encoding.UTF8.GetBytes(header.ToString());
}
}
}
我想做的是
var post_data = Encoding.UTF8.GetBytes(name = value );
var request = HttpRequest.create(new Uri(url));
request.method = verb.ToString() ;
使用(var ss = request。 get_stream())
ss.Write(post_data,0,post_data.Length);
我无法附加ss(流)有更多数据:(
I've been tried doing for a day or so but can't make it work !
I want to set an System.IO.Stream object for further usage ! like WebRequest.GetRequestStream()
any help would be appreciated !
What I have tried:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.IO;
namespace Utility
{
internal class HttpRequest
{
private string url;
public byte[] header_bytes;
protected const string crLf = "\r\n";
private Version _version = HttpVersion.Version11;
public NameValueCollection headers
{
get;
set;
}
public string method
{
get;
set;
}
public Version version
{
get { return _version; }
set { _version = value; }
}
public HttpRequest ( string url )
{
this.url = url;
headers = new NameValueCollection ();
}
public static HttpRequest create ( Uri uri )
{
var req = new HttpRequest (uri.PathAndQuery);
req.headers ["Host"] = uri.DnsSafeHost;
return req;
}
public Stream get_stream ()
{
get_header_bytes ();
return new MemoryStream(header_bytes);
}
public HttpResponse get_response ( Stream stream )
{
if ( header_bytes == null ) get_header_bytes ();
stream.Write (header_bytes , 0 , header_bytes.Length);
return HttpResponse.read (stream);
}
private void get_header_bytes ()
{
var header = new StringBuilder ();
header.AppendFormat ("{0} {1} HTTP/{2}{3}" , method , url , version , crLf);
foreach ( var key in headers.AllKeys )
header.AppendFormat ("{0}: {1}{2}" , key , headers [key] , crLf);
header.AppendLine ();
header_bytes = Encoding.UTF8.GetBytes (header.ToString ());
}
}
}
What i want to do is
var post_data = Encoding.UTF8.GetBytes("name=value");
var request = HttpRequest.create (new Uri (url));
request.method = verb.ToString ();
using ( var ss = request.get_stream () )
ss.Write (post_data , 0 , post_data.Length);
I can't append the ss (Stream) with more data :(
推荐答案
当你创建一个新的MemoryStream
传入一个byte
数组,该流的容量限制为您传入的字节数组的大小。
When you create a newMemoryStream
passing in abyte
array, the capacity of that stream is limited to the size of the byte array you pass in.
根据指定的字节数组初始化MemoryStream类的新不可调整大小的实例。
Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array.
您需要使用默认构造函数或指定初始容量的构造函数:
You need to use the default constructor, or the constructor that specifies the initial capacity:
var result = new MemoryStream(header_bytes.Length);
result.Write(header_bytes, 0, header_bytes.Length);
return result;
然后,您就可以向流中写入额外的字节。
You'll then be able to write additional bytes to the stream.
var temp_bytes = header_bytes;
header_bytes = new byte [1024];
var ret = new MemoryStream (header_bytes , 0 , 1024 , true);
ret.Write (temp_bytes , 0 , temp_bytes.Length);
return ret;
我终于找到了它!谢谢
I finally found it myself ! thank you
Richard Deeming
我深入了解内存流并找到解决方案!非常感谢
i dig deep into memorystream and found the solution ! really appreciated
这篇关于如何设置system.io.stream对象以写入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!