❔ HttpWebRequest
Here in below code what does stream.Write do ?
Does it add header to web request?
or Does it console log to server?
I`m not familiar with stream
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(TestUrl);
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/json";
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
24 Replies
That seems like a very archaic way to write the content of a post request
I don’t even think it would work given that the content type is application/json but the data is encoded with ASCII bytes
Everything about this request is odd
It might be done because the file size of the content is very large
I don’t understand why that would be the case though because the stream is written synchronously
yes,data contains login credentials
Can you please explain what stream.write will do to current request?
will it add to header,body, something else ?
there is no point in explaining, WebRequest is obsolete
Use HttpClient
@Henkypenky Currently using this in project.
Don`t want to change it but want to understand what it does
what version of .net does your project target?
.net framework 4.8
i see your problem
so, use httpclient and HttpRequestMessage class and add the headers that way
@Henkypenky What does stream.write do in above code?
as far as i can tell it opens up a stream to write request data, being the data itself, starting at 0 and providing the length of the data. so it will copy data to the stream, at position 0, with that length
but then you do nothing with that
so it's nonsense
that's what i understand, please someone correct me if i'm wrong
I`m not sure
but I am thinking stream will add to request.
but I am not sure
HttpWebRequest.GetRequestStream Method
Gets a Stream object to use to write request data.
does it make sense ?
that's what the docs say
So will the stream data will attach to request?
i don't know, maybe someone from the archaic times of WebRequest can answer, though i highly doubt it
https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.getrequeststream?view=net-7.0
HttpWebRequest.GetRequestStream Method (System.Net)
Gets a Stream object to use to write request data.
There is one example
i see
so
as far as i can tell
Stream.Write sends the data
I think most of answer present in above link
but I want to make sure that I understand it correctly
so you get the stream
write it
and it sends the data
then you close the stream
which you are already doing with the using
man, what a weird way of doing requests
yeah
When I read the docs,I feel the same way.
I was also confuse
so line by line:
create request object,
get the data you want to send
set the HTTP method
set the content type
open a stream and post the data
close the stream
get the response
Got it
Thanks
👍
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.