Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :(
Posted
Updated 14-Apr-16 11:59am
v2
Comments
Richard Deeming 14-Apr-16 15:20pm    
Your code samples are incomplete - what does get_header_bytes do, and where is header_bytes defined?

You also haven't told us what the problem is.
ZurdoDev 14-Apr-16 15:34pm    
What exactly is your question?
nazarinlaw 14-Apr-16 16:01pm    
Sorry for my bad english i just don't know a way to ask this ! i updated the question

When you create a new MemoryStream passing in a byte array, the capacity of that stream is limited to the size of the byte array you pass in.

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:
C#
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.
 
Share this answer
 
Comments
nazarinlaw 14-Apr-16 17:07pm    
I've tried it is not working ! Still can't append additional bytes :(
Richard Deeming 14-Apr-16 17:18pm    
Then you're not using the code I posted.

See this example[^] - writing additional bytes to the MemoryStream works perfectly.
nazarinlaw 14-Apr-16 17:33pm    
click me[^]

Please check my code and show me what i did wrong :)
Richard Deeming 14-Apr-16 17:35pm    
You're writing out the contents from t.bytes, not the MemoryStream:
Updated example[^]

In case you miss it, the difference is the third and finaly foreach loop.

You had:
foreach (byte b in t.bytes)
when it should be:
foreach (byte b in ms.ToArray())
nazarinlaw 14-Apr-16 17:40pm    
you are displaying your ms bytes not the one on class Test

Kindly check it please[^]
C#
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900