0

Below is the python code for getting cookies and it is worked as expected, and trying to convert this to C# but not getting the cookies. Any other way i can convert below Python code to respective C# code.

Python code:

data = {"login": user, "password": password}
response = session.post(url, headers=HTTP_HEADERS, data=data, verify = False)
response.raise_for_status()
result = session.cookies.get("auth_session")

C# code

CookieContainer Cookies = new CookieContainer();
String username = "xxx_priceyyy";
String password = "xxxx@6781119";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)); 
HttpWebRequest oRequest = WebRequest.Create("https://kube.xxx.com/") as HttpWebRequest;
oRequest.CookieContainer = Cookies;        
oRequest.Method = "POST"; 
oRequest.KeepAlive = false;
oRequest.Headers.Add("Authorization", "Basic " + encoded);              
Stream newStream = oRequest.GetRequestStream(); 
WebResponse oResponse = oRequest.GetResponse();
var responseString = new StreamReader(oResponse.GetResponseStream()).ReadToEnd();
mabiyan
  • 667
  • 7
  • 25
Shijas Km
  • 13
  • 4
  • Is this a .NET Core web project? or MVC or webform or an API Controller? – Zee Mar 10 '22 at 17:02
  • windows project and calling api for getting external data. Mainly looking for an alternative in C# for session.cookies.get("auth_session") – Shijas Km Mar 11 '22 at 03:55
  • Checkout this post https://stackoverflow.com/a/13318204/6048531 – Zee Mar 11 '22 at 04:05

1 Answers1

0

This link might be able to help you How to make an HTTP POST web request.

The recommended method of using httpClient over WebRequest is of particular note.

PsiMatrix
  • 61
  • 8