Click here to Skip to main content
15,882,055 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi All,

I am a .Net expert and don't know much details about Python (Used python.Net long back).

I came across below script from[^] site.

I would like to convert below code to C#, so I can create a small and handy application in C# for my parent.

import urllib2, urllib, cookielib

username = '1111111111111111' #replace the text within quotes with your username
password = 'password'   #replace the text within quotes with your password

jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))

response ="")

login_data = urllib.urlencode({'userId' : username, 'password' : password, 'action' : 'doLoginSubmit'})
resp = opene

Thank you for your help....

Rushikesh Joshi
Updated 16-Jul-23 1:24am
Sergey Alexandrovich Kryukov 6-Jul-12 14:22pm    
If you are a .NET expert, you don't need to translate anything, especially this short. You only need to know what do you want. Do you? :-)
bbirajdar 8-Jul-12 14:08pm    
Go ahead and convert it..

1 solution


using System;
using System.Net;
using System.Collections.Generic;

class Program
    static void Main(string[] args)
        string username = "1111111111111111";
        string password = "password";
        CookieContainer jar = new CookieContainer();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
        request.CookieContainer = jar;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        string responseText = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
        string loginData = $"userId={username}&password={password}&action=doLoginSubmit";
        byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(loginData);
        request = (HttpWebRequest)WebRequest.Create("");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = dataBytes.Length;
        request.CookieContainer = jar;
        System.IO.Stream requestBody = request.GetRequestStream();
        requestBody.Write(dataBytes, 0, dataBytes.Length);
        response = (HttpWebResponse)request.GetResponse();
        responseText = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
Share this answer
Graeme_Grant 16-Jul-23 10:50am    
The question is 11+ years old ... maybe focus on more current questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900