Created
May 12, 2015 14:50
-
-
Save gon250/ba8183ea4bd902168a9c to your computer and use it in GitHub Desktop.
access to a website(c#) from a class library and avoid .NET login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//GET COOKIE | |
var authCookie = _loginHelper.GetAuthCookie(email, password); | |
//SERIELIZE JSON | |
var jsonDetails = JsonConvert.SerializeObject(details); | |
//RESPONSE | |
var response = _portfolioHelper.HttpPost( _path, jsonDetails, authCookie); | |
var result = JsonConvert.DeserializeObject<Response<int?>>(response); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Project.Helpers | |
{ | |
public class HttpHelper | |
{ | |
public HttpHelper() { } | |
public string HttpPost(string path, string json, Cookie authCookie) | |
{ | |
var request = (HttpWebRequest)HttpWebRequest.Create(path); | |
//Proxy credentials | |
var proxy = WebRequest.DefaultWebProxy; | |
if (proxy != null) | |
{ | |
request.Proxy = proxy; | |
request.Credentials = CredentialCache.DefaultNetworkCredentials; | |
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; | |
} | |
request.Method = "POST"; | |
var cookieJar = new CookieContainer(); | |
cookieJar.Add(authCookie); | |
request.CookieContainer = cookieJar; | |
request.ContentType = "application/json"; | |
// write body | |
using (var streamWriter = new StreamWriter(request.GetRequestStream())) | |
{ | |
streamWriter.Write(json); | |
streamWriter.Flush(); | |
streamWriter.Close(); | |
} | |
// send request | |
var webResponse = request.GetResponse() as HttpWebResponse; | |
using (Stream stream = webResponse.GetResponseStream()) | |
{ | |
StreamReader reader = new StreamReader(stream, Encoding.UTF8); | |
String responseString = reader.ReadToEnd(); | |
return responseString; | |
} | |
} | |
public string HttpGet(string path, Cookie authCookie) | |
{ | |
var request = (HttpWebRequest)HttpWebRequest.Create(path); | |
//Proxy credentials | |
var proxy = WebRequest.DefaultWebProxy; | |
if (proxy != null) | |
{ | |
request.Proxy = proxy; | |
request.Credentials = CredentialCache.DefaultNetworkCredentials; | |
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; | |
} | |
request.Method = "GET"; | |
var cookieJar = new CookieContainer(); | |
cookieJar.Add(authCookie); | |
request.CookieContainer = cookieJar; | |
var webResponse = request.GetResponse() as HttpWebResponse; | |
using (Stream stream = webResponse.GetResponseStream()) | |
{ | |
StreamReader reader = new StreamReader(stream, Encoding.UTF8); | |
String responseString = reader.ReadToEnd(); | |
return responseString; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using HtmlAgilityPack; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
namespace Project.Helpers | |
{ | |
public class LoginHelper | |
{ | |
#if DEBUG | |
private const string _path = "your test path"; | |
#else | |
private const string _path = "your path"; | |
#endif | |
public LoginHelper() { } | |
public Cookie GetAuthCookie(string email, string password) | |
{ | |
var postData = String.Format("Email={0}&Password={1}&RememberMe={2}", email, password, false); | |
var request = (HttpWebRequest)HttpWebRequest.Create(_path); | |
request.AllowAutoRedirect = false; | |
//Proxy credentials | |
var proxy = WebRequest.DefaultWebProxy; | |
if (proxy != null) | |
{ | |
request.Proxy = proxy; | |
request.Credentials = CredentialCache.DefaultNetworkCredentials; | |
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; | |
} | |
request.Method = "POST"; | |
var cookieJar = new CookieContainer(); | |
request.CookieContainer = cookieJar; | |
request.ContentLength = postData.Length; | |
request.ContentType = "application/x-www-form-urlencoded"; | |
var myWriter = new StreamWriter(request.GetRequestStream()); | |
myWriter.Write(postData); | |
myWriter.Close(); | |
var webResponse = request.GetResponse() as HttpWebResponse; | |
var rawHeaders = request.GetResponse().Headers.ToString(); | |
return webResponse.Cookies[0]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment