【解決方法】FileCookieJar および urllib2 の Python から C# コードへの変換

プログラミングQA


こんにちは、みんな、

私は .Net の専門家ですが、Python についてはあまり詳しくありません (ずっと前に python.Net を使用していました)。

以下のスクリプトを見つけました http://www.kunaldua.com/blog/2009/12/reliance-wireless-broadband-auto-login-script/[^] サイト。

以下のコードを C# に変換して、親のために C# で小さくて便利なアプリケーションを作成したいと考えています。

C#
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 = opener.open("http://10.239.89.15/reliance/startportal_isg.do")

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

ご協力ありがとうございました….

ありがとう
Rushikesh Joshi

解決策 1

使用 www.codeconvert.ai/app[^]

C#
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("http://10.239.89.15/reliance/startportal_isg.do");
        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("http://10.239.89.15/reliance/startportal_isg.do");
        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);
        requestBody.Close();
        response = (HttpWebResponse)request.GetResponse();
        responseText = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
    }
}

コメント

タイトルとURLをコピーしました