I want to get a csv file from mint.com and manipulate it in C#. I have to log in to do this. I have followed the suggestions of another post on Stack Overflow and have come up with the following code. Unfortunately, the result I get is not the intended CSV but instead an error page. Any insight into what I am doing wrong or how I could do this another way?
string cookieHeader = CookieHeaderFromMintLogin("myusername","mypassword");
string csvURL = "https://wwws.mint.com/transactionDownload.event?queryNew=&offset=0&filterType=cash&comparableType=8";
string csv = getResponseWithCookies(csvURL, cookieHeader);
string CookieHeaderFromMintLogin(string username, string password)
{
string formUrl = "https://wwws.mint.com/loginJumper.event"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
string formBuilId = HttpUtility.UrlEncode("form-NAjKdtCT8Z5SuyDHTsmRREioaCMXVEJZpt8-9oaSMEY");
string formId = HttpUtility.UrlEncode("mint_auth_mint_com_login_form");
username = HttpUtility.UrlEncode(username);
password = HttpUtility.UrlEncode(password);
string formParams = string.Format("username={0}&password={1}&form_build_id={2}&form_id={3}", username, password, formBuilId,formId);
WebRequest req = WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
return resp.Headers["Set-cookie"];
}
string getResponseWithCookies(string getUrl, string cookieHeader)
{
string pageSource;
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
return pageSource;
}
Related
I'm trying to get html code of authorizated steam page, but I can't log in.
My code is
public string tryLogin(string EXP, string MOD, string TIME)
{
var rsa = new RSA();
string encryptedPass;
rsa.Exponent = EXP;
rsa.Modulus = MOD;
encryptedPass = rsa.Encrypt(Pass);
string reqString = "username=kasyjack&password=" + encryptedPass + "&emailauth=&kasyjackfriendlyname=&captchagid=&captcha_text=&emailsteamid=&rsatimestamp=" + TIME + "&remember_login=false";
byte[] requestData = Encoding.UTF8.GetBytes(reqString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://store.steampowered.com/login/dologin/");
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.ContentLength = requestData.Length;
request.Host = "store.steampowered.com";
request.Method = "POST";
request.UserAgent = Http.ChromeUserAgent();
request.CookieContainer = _CookieCont;
using (Stream st = request.GetRequestStream())
st.Write(requestData, 0, requestData.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string RSAR = sr.ReadToEnd();
return RSAR;
}
but response message is
{"success":false,"requires_twofactor":false,"clear_password_field":true,"captcha_needed":false,"captcha_gid":-1,"message":"Incorrect login."}
So does anyone know, what's wrong with login? Need your help.
SteamBot repository on GitHub has a working example on how to login to Steam via their website: link to method.
Here is a diagram explaining the whole process(made by me):
The question is clear I hope.
I want log in to DeviantArt through my application and I found stuff but it just doesn't seem to work...
Here's my code(running in a thread):
//===========================================
// Login
//===========================================
string formUrl = "https://www.deviantart.com/users/login";
string formParams = string.Format("login-username={0}&login-password={1}", "mai username", "mai password");
string cookieHeader;
System.Net.WebRequest req = System.Net.WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
//=========Send Cookie=============
System.Net.WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
string pageSource;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
string getUrl = "http://www.deviantart.com/users/loggedin";
System.Net.WebRequest getRequest = System.Net.WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
System.Net.WebResponse getResponse = getRequest.GetResponse();
I get a 403 error at the last line where I'm trying to get response. Some information about DeviantArt: 1: You can access pages etc. but you can't comment or anything. 2: When I tried to check if deviantart.com existed it gave me a 403 no permission error so that's why I'm trying to log in. When I write pageSource to a textfile I get this. What do I do wrong and how do I fix it? because I really have no clue why this isn't working...
PS: Alternate ways to do this are also welcome except Selenium
I would like to port a web browser form submittion to a httpwebrequest one.
its pretty simple:
wb.Document.GetElementsByTagName("input")[0].SetAttribute("value", "image.png");
wb.Document.GetElementsByTagName("input")[1].SetAttribute("value", ImageToBase64(p.Image));
wb.Document.GetElementsByTagName("form")[0].InvokeMember("submit");
I have tried the following, but the image file is currupted somehow...
string param = "name=image.jpg&file=" + ImageToBase64(p.Image);
post(param, "urlToPHPfileShown");
public string post(string param, string url)
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
string postArgs = param;
req.Method = "POST";
req.CookieContainer = new CookieContainer();
req.ContentType = #"application/x-www-form-urlencoded";
byte[] buffer = System.Text.ASCIIEncoding.UTF8.GetBytes(postArgs);
req.ContentLength = buffer.Length;
req.AllowAutoRedirect = true;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
}
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
using (Stream respStream = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(respStream))
{
string s = sr.ReadToEnd();
return s;
}
}
}
EDIT:
I just looked at the post with WireShark.
The one which is working seem to have lots of %2F instead of just / (slash).
So is there maybe a charset problem?
I'm trying to replicate the process detailed here; https://developers.google.com/accounts/docs/OAuth2WebServer#handlingtheresponse
in C#
String authorizationCode = String.Empty;
String consumerKey = String.Empty;
String consumerSecret = String.Empty;
String redirectUrl = String.Empty;
String grantType = String.Empty;
String requestContent = String.Empty;
HttpWebRequest request = null;
byte[] byteArray = null;
Stream dataStream = null;
WebResponse response = null;
StreamReader reader = null;
String serverResponse = String.Empty;
byte[] authorizationResult = null;
try
{
authorizationCode = HttpUtility.UrlEncode(context.Request.QueryString["code"]);
consumerKey = Properties.Settings.Default.GoogleConsumerKey;
consumerSecret = Properties.Settings.Default.GoogleConsumerSecret;
redirectUrl = Properties.Settings.Default.RedirectUrl;
grantType = "authorization_code";
request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
request.Method = "POST";
requestContent = String.Format("code={0}&client_id={1}&client_secret={2}&redirect_url={3}&grant_type={4}",authorizationCode,consumerKey,consumerSecret,redirectUrl,grantType);
byteArray = Encoding.UTF8.GetBytes(requestContent);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
response = request.GetResponse();
dataStream = response.GetResponseStream();
reader = new StreamReader(dataStream);
serverResponse = HttpUtility.UrlDecode(reader.ReadToEnd());
reader.Close();
dataStream.Close();
response.Close();
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
}
The Trouble is when calling GetResponse() I am getting Bad Request.
The ConsumerKey & Secret is the one I got from Google when I registered my application. The authorizationCode comes from Google as well.
any ideas what I am doing wrong?
Thanks in advance.
I had the same issue :
The "using" keyword solved it for me. follow the link:
https://stackoverflow.com/a/1968543/1820776
Here's my code for Request and Response.
System.IO.MemoryStream xmlStream = null;
HttpWebRequest HttpReq = (HttpWebRequest)WebRequest.Create(url);
xmlStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(format));
byte[] buf2 = xmlStream.ToArray();
System.Text.UTF8Encoding UTF8Enc = new System.Text.UTF8Encoding();
string s = UTF8Enc.GetString(buf2);
string sPost = "XMLData=" + System.Web.HttpUtility.UrlDecode(s);
byte[] bPostData = UTF8Enc.GetBytes(sPost);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
HttpReq.Timeout = 30000;
request.Method = "POST";
request.KeepAlive = true;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bPostData, 0, bPostData.Length);
requestStream.Close();
}
string responseString = "";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
responseString = sr.ReadToEnd();
}
No part of this code crashes. The "format" string is the one with XML in it. By the end when you try to see what's in the responseString, it's an empty string. I am supposed to see the XML sent back to me from the URL. Is there something missing in this code?
I would recommend a simplification of this messy code:
using (var client = new WebClient())
{
var values = new NameValueCollection
{
{ "XMLData", format }
};
byte[] resultBuffer = client.UploadValues(url, values);
string result = Encoding.UTF8.GetString(resultBuffer);
}
and if you wanted to upload the XML directly in the POST body you shouldn't be using application/x-www-form-urlencoded as content type. You probably should specify the correct content type, like this:
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "text/xml";
var data = Encoding.UTF8.GetBytes(format);
byte[] resultBuffer = client.UploadData(url, data);
string result = Encoding.UTF8.GetString(resultBuffer);
}