Asp.net core web request

I want to make a web request in the main asp.net project. I tried the following, but it doesn't seem to send data to the request:

using System.Net;

...

//encoder
UTF8Encoding enc = new UTF8Encoding();

//data
string data = "[\"some.data\"]";

//Create request
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.Credentials = new NetworkCredential(user, secret);

//Set data in request
Stream dataStream = await request.GetRequestStreamAsync();
dataStream.Write(enc.GetBytes(data), 0, data.Length); 


//Get the response
WebResponse wr = await request.GetResponseAsync();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();

I do not receive an error message, the request was sent, but it does not send data with the request. I also can not provide the length of the data with the request. Is this a major issue? (ps: credentials are sent correctly)

Can anybody help me?

+4
source share
2 answers

Finally decided. They were a bug in my external api code where I resolved the api request. The code in my question works (if someone wants to use it).

PS: I edit the code with ycrumeyrolle comment

0
source

. , GetRequestStreamAsync() GetResponseAsync(), Result.

//Set data in request
Stream dataStream = await request.GetRequestStreamAsync();

//Get the response
WebResponse wr = await request.GetResponseAsync();
0

Source: https://habr.com/ru/post/1663943/


All Articles