C # socket programming in ASP.net

Is it possible that writing a Socket in C # in ASP.net? as an example Can I write code Like this Perl code in C # and asp.net ?:

> use HTTP::Request::Common qw(POST);
> use LWP::UserAgent; $ua = new
> LWP::UserAgent(agent => 'Mozilla/5.0(Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5)Gecko/20060719 Firefox/1.5.0.5'); 
> $ua -> timeout(20);
> my $req = POST 'http://Example.com/',
> [ login_username => 'mehdi' , login22 => '654321' , go => 'submit']; 
> my $content = $ua->request($req);

please give me an example or convert the above code to c # and asp.net. Thanks in advance.

+3
source share
1 answer

Yes, you could achieve the same functionality in .NET using the WebClient class :

class Program
{
    static void Main()
    {
        using (var client = new WebClient())
        {
            client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0(Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5)Gecko/20060719 Firefox/1.5.0.5";
            var values = new NameValueCollection
            {
                { "login_username", "mehdi" },
                { "login22", "654321" },
                { "go", "submit" }
            };
            var result = client.UploadValues("http://example.com", values);

            // TODO: handle the result here like
            Console.WriteLine(Encoding.Default.GetString(result));
        }
    }
}
+6
source

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


All Articles