C
C#2y ago
Lunar

❔ 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
Connor
Connor2y ago
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
Lunar
LunarOP2y ago
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 ?
Henkypenky
Henkypenky2y ago
there is no point in explaining, WebRequest is obsolete Use HttpClient
Lunar
LunarOP2y ago
@Henkypenky Currently using this in project. Don`t want to change it but want to understand what it does
Henkypenky
Henkypenky2y ago
what version of .net does your project target?
Lunar
LunarOP2y ago
.net framework 4.8
Henkypenky
Henkypenky2y ago
i see your problem so, use httpclient and HttpRequestMessage class and add the headers that way
Lunar
LunarOP2y ago
@Henkypenky What does stream.write do in above code?
Henkypenky
Henkypenky2y ago
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
Lunar
LunarOP2y ago
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 ?
Henkypenky
Henkypenky2y ago
that's what the docs say
Lunar
LunarOP2y ago
So will the stream data will attach to request?
Henkypenky
Henkypenky2y ago
i don't know, maybe someone from the archaic times of WebRequest can answer, though i highly doubt it
Lunar
LunarOP2y ago
There is one example
Henkypenky
Henkypenky2y ago
i see so as far as i can tell Stream.Write sends the data
Lunar
LunarOP2y ago
I think most of answer present in above link but I want to make sure that I understand it correctly
Henkypenky
Henkypenky2y ago
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
Lunar
LunarOP2y ago
yeah When I read the docs,I feel the same way. I was also confuse
Henkypenky
Henkypenky2y ago
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
Lunar
LunarOP2y ago
Got it Thanks
Henkypenky
Henkypenky2y ago
Ok
Lunar
LunarOP2y ago
👍
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server