C
C# Learner
I've been learning .NET for a couple of weeks now.
I've found that on the surface, .NET looks powerful and very handy.
However, when getting down and dirty with it, I've found that
sometimes it's no longer the beast it first appeared. It then shows
itself to be irrationally impotent in certain aspects.
Here are a couple of examples:
Example 1
---------
An HttpWebRequest instance cannot be given a "raw" HTTP message. I
see absolutely no reason why this is so, since somewhere along the
line of an HttpWebRequest instance sending a HTTP request, a raw HTTP
message will be sent.
Instead, if one has a raw HTTP message in the first place, one must
parse the raw message (whether it be held in string, byte[], etc.)
into an HttpWebRequest instance.
Why should the user of .NET have to do this? This is ridiculous. In
this case extra processing is involved:
1) User code parses raw HTTP message and builds a new HttpWebRequest
instance from it.
2) HttpWebRequest code then does the complete opposite to get a raw
HTTP message.
3) HttpWebRequest then sends raw HTTP message.
Instead, the following should happen:
1) HttpWebRequest has the ability to send a raw HTTP message, given to
it by user.
Example 2
---------
One cannot retrieve the "raw" HTTP message from an HttpWebResponse.
Again, I see no reason why this should be so, since somewhere along
the line of getting an HTTP response a raw HTTP message is received.
Instead, one must do something like the following:
byte[] headerBytes = response.Headers.ToByteArray();
Then one must somehow prepend the string "HTTP 200 OK\r\n" to those
bytes. Next, of course, one must append the response body.
An example of the clumsiness involved:
string statusString = "HTTP/1.1 200 OK\r\n";
byte[] statusBytes = Encoding.ASCII.GetBytes(statusString);
byte[] headerBytes = response.Headers.ToByteArray();
byte[] bodyBytes = ReadFully(response.GetResponseStream());
// ReadFully is implemented as (obtained from Jon Skeet's site):
public static byte[] ReadFully(Stream stream)
{
byte[] buffer = new byte[32768];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0,
buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}
Here, we have obtained three byte arrays which add up to a full HTTP
message.
Why can't we simply say the following?
byte[] messageBytes = response.GetRawMessage();
That would be so much nicer, and would be easy to implement. Perhaps
even the following might be more logical:
byte[] messageBytes = response.RawMessage;
....depending on whether an HttpResponse stores the raw message using a
byte[] instance.
Further, why should I need to read from one stream, write to another,
and then, finally, convert this stream to a byte array?
That is bound to be rather inefficient, not to mention the fact that
it takes the implementation of a whole new method.
I've found that on the surface, .NET looks powerful and very handy.
However, when getting down and dirty with it, I've found that
sometimes it's no longer the beast it first appeared. It then shows
itself to be irrationally impotent in certain aspects.
Here are a couple of examples:
Example 1
---------
An HttpWebRequest instance cannot be given a "raw" HTTP message. I
see absolutely no reason why this is so, since somewhere along the
line of an HttpWebRequest instance sending a HTTP request, a raw HTTP
message will be sent.
Instead, if one has a raw HTTP message in the first place, one must
parse the raw message (whether it be held in string, byte[], etc.)
into an HttpWebRequest instance.
Why should the user of .NET have to do this? This is ridiculous. In
this case extra processing is involved:
1) User code parses raw HTTP message and builds a new HttpWebRequest
instance from it.
2) HttpWebRequest code then does the complete opposite to get a raw
HTTP message.
3) HttpWebRequest then sends raw HTTP message.
Instead, the following should happen:
1) HttpWebRequest has the ability to send a raw HTTP message, given to
it by user.
Example 2
---------
One cannot retrieve the "raw" HTTP message from an HttpWebResponse.
Again, I see no reason why this should be so, since somewhere along
the line of getting an HTTP response a raw HTTP message is received.
Instead, one must do something like the following:
byte[] headerBytes = response.Headers.ToByteArray();
Then one must somehow prepend the string "HTTP 200 OK\r\n" to those
bytes. Next, of course, one must append the response body.
An example of the clumsiness involved:
string statusString = "HTTP/1.1 200 OK\r\n";
byte[] statusBytes = Encoding.ASCII.GetBytes(statusString);
byte[] headerBytes = response.Headers.ToByteArray();
byte[] bodyBytes = ReadFully(response.GetResponseStream());
// ReadFully is implemented as (obtained from Jon Skeet's site):
public static byte[] ReadFully(Stream stream)
{
byte[] buffer = new byte[32768];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0,
buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}
Here, we have obtained three byte arrays which add up to a full HTTP
message.
Why can't we simply say the following?
byte[] messageBytes = response.GetRawMessage();
That would be so much nicer, and would be easy to implement. Perhaps
even the following might be more logical:
byte[] messageBytes = response.RawMessage;
....depending on whether an HttpResponse stores the raw message using a
byte[] instance.
Further, why should I need to read from one stream, write to another,
and then, finally, convert this stream to a byte array?
That is bound to be rather inefficient, not to mention the fact that
it takes the implementation of a whole new method.