Currently I'm building a c# based desktop application. Its like a small web form auto filler. The application make GET and POST calls for getting the images in responsestream. But I'm getting very slow response. Any suggestions on how I can increase the speed of image download. Following is the code for downloading the image and POST call
Image Download Function:
public void download_image(string go_to_url, string referer)
{
this.httpWebRequest_cls_1 = null;
HttpWebResponse response = null;
try
{
this.httpWebRequest_cls_1 = (HttpWebRequest)WebRequest.Create(go_to_url);
this.httpWebRequest_cls_1.Proxy = null;
WebRequest.DefaultWebProxy = null;
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
this.httpWebRequest_cls_1.ServicePoint.UseNagleAlgorithm = false;
this.httpWebRequest_cls_1.ServicePoint.Expect100Continue = false;
this.httpWebRequest_cls_1.ServicePoint.ConnectionLimit = 100000;
this.httpWebRequest_cls_1.ServicePoint.ConnectionLeaseTimeout = 65000;
this.httpWebRequest_cls_1.ServicePoint.MaxIdleTime = 100000;
this.httpWebRequest_cls_1.CookieContainer = global_store.cookieContainer_0;
this.httpWebRequest_cls_1.Referer = str1;
this.httpWebRequest_cls_1.KeepAlive = true;
this.httpWebRequest_cls_1.ReadWriteTimeout = 0xc350;
this.httpWebRequest_cls_1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
this.httpWebRequest_cls_1.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
this.httpWebRequest_cls_1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36";
this.httpWebRequest_cls_1.UnsafeAuthenticatedConnectionSharing = true;
this.httpWebRequest_cls_1.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
this.httpWebRequest_cls_1.PreAuthenticate = true;
this.httpWebRequest_cls_1.AllowWriteStreamBuffering = false;
this.httpWebRequest_cls_1.Pipelined = false;
this.httpWebRequest_cls_1.ProtocolVersion = HttpVersion.Version11;
this.httpWebRequest_cls_1.ContentType = "application/x-www-form-urlencoded";
this.httpWebRequest_cls_1.Headers.Set("Cache-Control", "max-age=86400");
this.httpWebRequest_cls_1.ContentLength = 0L;
this.httpWebRequest_cls_1.Method = "GET";
response = (HttpWebResponse)this.httpWebRequest_cls_1.GetResponse();
global_store.image_0 = Image.FromStream(response.GetResponseStream());
staticstore.get_cap_time = new TimeSpan(DateTime.Now.Hour,
DateTime.Now.Minute, DateTime.Now.Second);
}
catch (WebException exception)
{
string exp = exception.ToString();
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
if (this.httpWebRequest_cls_1 != null)
{
this.httpWebRequest_cls_1.Abort();
this.httpWebRequest_cls_1 = null;
}
}
}
POST Request Sending Function:
public string simple_web_call(string go_to, string from, string params_to_post, string conditions)
{
string str = "";
string responseHeader = "";
httpWebRequest_3 = null;
Stream requestStream = null;
HttpWebResponse response = null;
Stream responseStream = null;
try
{
this.httpWebRequest_3 = (HttpWebRequest)WebRequest.Create(new Uri(go_to));
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
this.httpWebRequest_3.CachePolicy = policy;
this.httpWebRequest_3.Proxy = null;
WebRequest.DefaultWebProxy = null;
this.httpWebRequest_3.ServicePoint.UseNagleAlgorithm = false;
this.httpWebRequest_3.ServicePoint.Expect100Continue = false;
this.httpWebRequest_3.ServicePoint.ConnectionLimit = 65000;
this.httpWebRequest_3.ServicePoint.ConnectionLeaseTimeout = Class9.int_7;
this.httpWebRequest_3.ServicePoint.MaxIdleTime = 10000;
this.httpWebRequest_3.CookieContainer = Class28.cookieContainer_0;
this.httpWebRequest_3.Referer = from;
this.httpWebRequest_3.KeepAlive = true;
this.httpWebRequest_3.Connection = "keepalive";
this.httpWebRequest_3.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
this.httpWebRequest_3.UserAgent = "Mozilla/5.0 (Windows T 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36";
this.httpWebRequest_3.Headers.Set("Cache-Control", "no-cache");
this.httpWebRequest_3.UnsafeAuthenticatedConnectionSharing = true;
this.httpWebRequest_3.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
this.httpWebRequest_3.ProtocolVersion = HttpVersion.Version11;
this.httpWebRequest_3.Headers.Set("Cache-Control", "no-cache");
if (params_to_post != "")
{
this.httpWebRequest_3.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(params_to_post);
this.httpWebRequest_3.ContentType = "application/x-www-form-urlencoded";
this.httpWebRequest_3.ContentLength = bytes.Length;
requestStream = this.httpWebRequest_3.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
}
else
{
this.httpWebRequest_3.Method = "GET";
}
response = (HttpWebResponse)this.httpWebRequest_3.GetResponse();
HttpWebResponse response2 = response;
switch (response2.StatusCode)
{
case HttpStatusCode.OK:
{
responseStream = response2.GetResponseStream();
str = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd();
responseStream.Close();
responseStream = null;
goto flush_all;
}
case HttpStatusCode.MovedPermanently:
case HttpStatusCode.Found:
case HttpStatusCode.SeeOther:
case HttpStatusCode.TemporaryRedirect:
responseHeader = response2.GetResponseHeader("Location");
if (!responseHeader.Contains("err")) { break; }
str = "retry";
goto flush_all;
default:
str = "retry";
goto flush_all;
}
str = responseHeader;
flush_all:
response2 = null;
response.Close();
response = null;
this.httpWebRequest_3 = null;
if (str == "") { str = "retry"; }
}
catch (WebException exception)
{
string exp = exception.ToString();
}
finally
{
if (requestStream != null)
{
requestStream.Close();
requestStream.Dispose();
requestStream = null;
}
if (responseStream != null)
{
responseStream.Close();
responseStream.Dispose();
responseStream = null;
}
if (response != null)
{
response.Close();
response = null;
}
if (this.httpWebRequest_3 != null)
{
this.httpWebRequest_3.Abort();
this.httpWebRequest_3 = null;
}
}
return str;
}
Related
Hi I'm trying to login via https://www.strava.com/session with HttpWebrequest but it doesn't log me in. It gives me an response of 302 which is good but it never redirect me to https://www.strava.com/dashboard.
this is the code that I'm using
Httpclient:
public class HttpClient
{
private const string UserAgent = "Mozilla/5.0";
public CookieCollection CookieCollection;
public HttpWebRequest WebRequest;
public HttpWebResponse WebResponse;
public int code { get; set; }
public string location { get; set; }
public string PostData(string url, string postData, string refer = "")
{
WebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
WebRequest.UserAgent = UserAgent;
WebRequest.Referer = refer;
WebRequest.AllowAutoRedirect =false;
WebRequest.Timeout = 10000;
WebRequest.KeepAlive = true;
WebRequest.CookieContainer = new CookieContainer();
if (CookieCollection != null && CookieCollection.Count > 0)
{
WebRequest.CookieContainer.Add(CookieCollection);
}
WebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
WebRequest.Method = "POST";
try
{
var postBytes = Encoding.UTF8.GetBytes(postData);
WebRequest.ContentLength = postBytes.Length;
var postDataStream = WebRequest.GetRequestStream();
postDataStream.Write(postBytes, 0, postBytes.Length);
postDataStream.Close();
try
{
WebResponse = (HttpWebResponse)WebRequest.GetResponse();
this.code = (int)WebResponse.StatusCode;
this.location = WebResponse.Headers["Location"];
if (WebResponse.StatusCode == HttpStatusCode.OK ||WebResponse.StatusCode == HttpStatusCode.Redirect)
{
WebResponse.Cookies = WebRequest.CookieContainer.GetCookies(WebRequest.RequestUri);
if (WebResponse.Cookies.Count > 0)
{
if (CookieCollection == null)
{
CookieCollection = WebResponse.Cookies;
}
else
{
foreach (Cookie oRespCookie in WebResponse.Cookies)
{
var bMatch = false;
foreach (
var oReqCookie in
CookieCollection.Cast<Cookie>()
.Where(oReqCookie => oReqCookie.Name == oRespCookie.Name))
{
oReqCookie.Value = oRespCookie.Value;
bMatch = true;
break;
}
if (!bMatch)
CookieCollection.Add(oRespCookie);
}
}
}
var reader = new StreamReader(WebResponse.GetResponseStream());
var responseString = reader.ReadToEnd();
reader.Close();
return responseString;
}
}
catch (WebException wex)
{
if (wex.Response != null)
{
using (var errorResponse = (HttpWebResponse)wex.Response)
{
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
{
var error = reader.ReadToEnd();
return error;
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return "Error in posting data" ;
}
public string GetData(string url, string post = "")
{
var responseStr = string.Empty;
WebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
WebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
WebRequest.Method = "GET";
WebRequest.KeepAlive = true;
WebRequest.Credentials = CredentialCache.DefaultCredentials;
WebRequest.UserAgent = UserAgent;
WebRequest.CookieContainer = new CookieContainer();
if (CookieCollection != null && CookieCollection.Count > 0)
{
WebRequest.CookieContainer.Add(CookieCollection);
}
if (!string.IsNullOrEmpty(post))
{
var postBytes = Encoding.UTF8.GetBytes(post);
WebRequest.ContentLength = postBytes.Length;
var postDataStream = WebRequest.GetRequestStream();
postDataStream.Write(postBytes, 0, postBytes.Length);
postDataStream.Close();
}
WebResponse wresp = null;
try
{
wresp = WebRequest.GetResponse();
var downStream = wresp.GetResponseStream();
if (downStream != null)
{
using (var downReader = new StreamReader(downStream))
{
responseStr = downReader.ReadToEnd();
}
}
return responseStr;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
WebRequest = null;
}
return responseStr;
}
}
Getting crfs-token:
private string GetToken()
{
string token = "";
String sourcestring = hc.GetData(loginURL);
Regex metaTag = new Regex(#"<meta[\s]+[^>]*?name[\s]?=[\s""']+(.*?)[\s""']+content[\s]?=[\s""']+(.*?)[""']+.*?>");
foreach (Match m in metaTag.Matches(sourcestring))
{
if (m.Groups[2].Value.Contains("token"))
{
continue;
}
token = m.Groups[2].Value;
}
return token;
}
Custom keyvaluepair
private string PostParam(Dictionary<string, string> data)
{
var sb = new StringBuilder();
var p = new List<string>();
foreach (KeyValuePair<string, string> pair in data)
{
sb.Clear();
sb.Append(pair.Key).Append("=").Append(pair.Value);
p.Add(sb.ToString());
}
var pp = string.Join("&", p);
return pp;
}
Login:
private HttpClient hc = new HttpClient();
Dictionary data = new Dictionary();
data.Add("utf8", "✓");
data.Add("authenticity_token", GetToken());
data.Add("plan", "");
data.Add("email", "email");
data.Add("password", "password");
hc.PostData(sessionURL,WebUtility.UrlEncode(PostParam(data)), loginURL);
Can someone tell me what I'm doing wrong? if I look the request header when trying to login to strava in browser its the same but still it doesn't log me.
I found the problem.
You need to encode only the token (and the UTF8 character), not the full post data.
This works for me (for some reason I need to run the code two times)
// First time says "logged out"
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("utf8", WebUtility.UrlEncode("✓"));
string token = GetToken();
string tokenEncoded = WebUtility.UrlEncode(token);
data.Add("authenticity_token", tokenEncoded);
data.Add("plan", "");
data.Add("email", "youremail");
data.Add("password", "yourpwd");
data.Add("remember_me", "on");
string parameters = PostParam(data);
hc.PostData(sessionURL, parameters, loginURL);
// Second time logs in
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("utf8", WebUtility.UrlEncode("✓"));
string token = GetToken();
string tokenEncoded = WebUtility.UrlEncode(token);
data.Add("authenticity_token", tokenEncoded);
data.Add("plan", "");
data.Add("email", "youremail");
data.Add("password", "yourpwd");
data.Add("remember_me", "on");
string parameters = PostParam(data);
hc.PostData(sessionURL, parameters, loginURL);
Note:
// Keep this on default value "true"
//WebRequest.AllowAutoRedirect = false;
remark: you can use this code (see my previous post) to change activity's status (privacy) after logging in:
Dictionary<string, string> data2 = new Dictionary<string, string>();
data2.Add("utf8", WebUtility.UrlEncode("✓"));
string token2 = GetToken();
string tokenEncoded2 = WebUtility.UrlEncode(token2);
data2.Add("_method", "patch");
data2.Add("authenticity_token", tokenEncoded2);
data2.Add("activity%5Bvisibility%5D", "only_me"); // or "followers_only"
string parameters2 = PostParam(data2);
hc.PostData("https://www.strava.com/activities/youractivityID", parameters2, loginURL);
I'm going to get information from https web pages. But the downloaded data is coded and illegible.
\u001f�\b\0\0\0\0\0\0\0�iw۸�0�Y�\u0015\u0018��(y�z��,v���rܝ\u0019�v�N/�s��P\",3�H]��r��\u007f\u007fQ�\b���N���\u0011Il�B��\0\u0014�^�#�ŋ\u0017����\u007f���Gt��\u0003t����S\u001f9k\u001b\u001b\u007fl�76\u000e/\u000fџ�^~>F[��2v��O�(t����'\u000er��t���qww�~��\u001eţ���{�k\v\n�ǵT)��}�\u001eR�\a�^�E��\u0018�.����?���\a�,vGc�A�(Lq�~p�hm�\u000e���a�߇��>�\u001dGA�b\u001f�'~�\u0013���\u0016�\n\bB�/��\K\u001f&X)���t\u0003z����n���×ˣ��\u000e\"�YkjOh\u001f۲��\0�]\���\rY?\u001b\u0015u�����%��\0w�o��{����\u000fo�u��>8��\u001b�$�\b\"��_\lv6�v6�ެ�o\u000e�q��I҇\0'�
How to get the SSL certificate into the HttpWebRequest class?
class MyHttpWebRequest : IDisposable
{
private static int Count = 0;
private HttpWebRequest request;
private Stream dataStream;
private string Status { get; set; }
private string userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
private string accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
private string acceptLanguage = "Accept-Language: en-US,en;q=0.9";
private string acceptEncoding = "Accept-Encoding: gzip, deflate, br";
private string host;
private X509Certificate2 clientCertificates;
private String referer { get; set; } = "";
private CookieCollection cookieCollection { get; set; } = null;
public MyHttpWebRequest()
{
Count++;
clientCertificates = new X509Certificate2(#"mahan.cer");
//clientCertificates = X509Certificate.CreateFromCertFile(#"mahan.cer");//(#"LocalAuthority.crt");
//ServicePointManager.Expect100Continue = true;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
//ServicePointManager.Expect100Continue = false;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
//WebRequestHandler handler = new WebRequestHandler();
//X509Certificate2 certificate = GetMyX509Certificate();
//handler.ClientCertificates.Add(certificate);
//HttpClient client = new HttpClient(handler);
}
public MyHttpWebRequest(string host) : this()
{
this.host = host;
}
~MyHttpWebRequest()
{
Count--;
}
public string GetResponse()
{
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (cookieCollection == null) cookieCollection = new CookieCollection();
cookieCollection.Add(response.Cookies);
this.Status = response.StatusDescription;
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
//dataStream.Close();
response.Close();
//clientCertificates = new X509Certificate2(request.ServicePoint.Certificate);
this.Status = "Successful";
return responseFromServer;
}
catch (Exception ex)
{
this.Status = ex.Message;
return ex.ToString();
}
}
public string SetRequest(string url)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.ToString();
}
return this.ToString();
}
public string SetRequest(string url, string method)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
if (method.Equals("GET") || method.Equals("POST"))
{
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.ToString();
}
return this.ToString();
}
public string SetRequest(string url, string method, string data)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
if (method.Equals("GET") || method.Equals("POST"))
{
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
byte[] byteArray = Encoding.UTF8.GetBytes(data);
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.Message;
}
return this.Status;
}
public string SetRequest(string url, string method, string data, string contentType)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
if (method.Equals("GET") || method.Equals("POST"))
{
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = contentType;
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
byte[] byteArray = Encoding.UTF8.GetBytes(data);
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.Message;
}
return this.Status;
}
public void Dispose()
{
request.Abort();
request = null;
dataStream.Close();
dataStream.Dispose();
dataStream = null;
}
}
How to get the SSL certificate into the HttpWebRequest class?
Your problem is not related to SSL at all.
private string acceptEncoding = "Accept-Encoding: gzip, deflate, br";
...
request.Headers.Add(acceptEncoding);
With this code you explicitly tell the server that you'll support various content compression algorithms. Only, this is a lie since you don't deal with compression when reading the response.
\u001f�\b\0\0\0\0\0\0\0
This looks the beginning of a gzip data stream. It starts with hex \x1f\x8b but you interpret this wrongly as UTF-8 which it is not.
The easiest way to fix this part is to remove the Accept-Encoding from your request or use Accept-Encoding: identity to signal that you don't accept any compression. This way a well behaving server will send you the body without any compression.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://yourwebsite.nld");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
X509Certificate certV1 = request.ServicePoint.Certificate;
Certificate is a X509Certificate not a X509Certificate2.
To convert add
X509Certificate2 certV2 = new X509Certificate2(certV1);
I'm getting a json from httpWebResponse which supposed to be in the following format :
{"d":"{\"Success\":\"\",\"PricedItineraries\":[{\"IsRecommendedFlight\":false,\"BookingClass\":null,\"AirItinerary\":{\"OriginDestinationOptions\":{\"OriginDestinationOption\":[{\"FlightSegment\":[{\"DepartureAirport\":{\"LocationCode\":\"RAK\",\"Terminal\":\"1\"},\"ArrivalAirport\":{\"LocationCode\":\"ORY\",\"Terminal\":\"S\"},\"Equipment\":{\"AirEquipType\":\"73H\",\"ChangeofGauge\":\"true\"},\"MarketingAirline\":{\"Code\":\"TO\",\"CodeContext\":\"KBATO\"},\"OperatingAirline\":{\"Code\":\"TO\"},\"BookingClassAvails\":[{\"ResBookDesigCode\":\"A\",\"RPH\":\"5\"}],\"BagAllownceInfo\":{\"Allowance\":\"00\",\"QuantityCode\":\" N\",\"UnitQualifier\":\" K\"},\"FareID\":\"000000000\",\"Token\":\"00000000-0000-0000-0000-000000000000\",\"AdultBaseFare\":\"0000517.77\",\"AdultTaxFare\":\"0000000.00\",\"ChildBaseFare\":\"0000000.00\",\"ChildTaxFare\":\"0000000.00\",\"InfantBaseFare\":\"0000000.00\",\"InfantTaxFare\":\"0000000.00\",\"PriceTotal\":\"0000307.90\",\"LFID\":\"0000000\",\"PFID\":\"00000\",\"PTFID\":\"
I tried to Deserialize the json using JSonConverter but it returns stream was not readable:
public static object DeserializeFromStream(Stream stream)
{
var serializer = new JsonSerializer();
using (var sr = new StreamReader(stream))
using (var jsonTextReader = new JsonTextReader(sr))
{
return serializer.Deserialize(jsonTextReader);
}
}
So I'm reading my httpwebresponse:
using (StreamReader Reader = new StreamReader(ResStream))
{
this.ResponseHTML = Reader.ReadToEnd();
}
But somehow it returns:
?W?S???"??yE,?????♣*Ay◄?lA+??F◄K?\?b;^~§????xN?yU,6U,?☺♣U,T??O?♠V►R?B§??*????↨??
e?-|T?P?b???s§???M§♂U,?'??*?^le?????▬????§%7↕???f??Qd←|?c♣??
bq7;???ffv%)?▬??↔L6???s?V?~?#?$♀]☺◄????D????'X?e?_?????"??E??Q]E,Ad-? ♥?Xb'??K
[???y;?d"0??:?-X??←Xòs←▬?→?$?↑-b☼}E,???"??MF??j↨??;vb)?aq?ai???R?.5,???????→▬jX?
♂5,?↑j??P?2?→?????+5,?tu?#??ev??☺7;???o☺??3w???P?B
w???E?4!?→??MF??j??v?→▬?→???]jX?I?R???⌂?i]?5,a↕☼↓????→▬???kX??mjX?a◄1?♥'P?;??(??
♂??????H?aq??→▬??↑e???[§???%M?.^??}X???z??t??an?→▬c????_~Z?→▬jX?a?gZ?→▬jX?a?????
♂5,?↑j??P?2?→▬?a)Y??a????>,
5,??a?P?Ro▼▬?→???]jX?I????%t-jX8K?→▬jXLI?a?,!jX??%??PO??P?R?f???↔/4?f?c?7;?jX6?L
I'm sending the webrequest :
public Response Send()
{
if (REQUEST == null)
return new Response(REQUEST) { HTTPStatusCode = "999" };
WebResponse Res = null;
CookieContainer Cookies = new CookieContainer();
if (REQUEST.Cookies != null)
{
Cookies = REQUEST.Cookies;
}
bool isdone = true;
DateTime Time = default(DateTime);
Sender = (HttpWebRequest)WebRequest.Create(this.REQUEST._url);
Sender.Host = REQUEST.Host;
Sender.Accept = REQUEST.Accept;
Sender.Method = REQUEST.Method;
Sender.UserAgent = REQUEST.UserAgent;
Sender.ContentType = REQUEST.ContentType;
Sender.CookieContainer = Cookies;
Sender.Referer = REQUEST.Refer;
if (REQUEST.Data != null && REQUEST.Data.Length > 0)
{
using (var writer = new StreamWriter(Sender.GetRequestStream()))
{
writer.Write(REQUEST.Data);
writer.Flush();
writer.Close();
}
}
try
{
Res = Sender.GetResponse();
Time = DateTime.Now;
}
catch (Exception Ex)
{
isdone = false;
if (OnExceptionHappened != null)
OnExceptionHappened(this, new ExceptionArgs { Msg = Ex.Message, Sender = this, Time = DateTime.Now });
}
return AssignWebResponse((HttpWebResponse)Res, isdone, Time, Cookies, Sender.RequestUri.AbsoluteUri);
}
Request :
var REQUEST = new Request()
{
_url = "https://www.example.com/",
Host = "host",
Method = "POST",
Refer = "refer",
UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0",
ContentType = "application/json",
Cookies = CurrentContainer,
Data = "{ 'isFromCache': 'undefined', 'pageNumber': '1'}",
isJson = true
};
using (BeRequest.CORE.BeRequest Req = new BeRequest.CORE.BeRequest(REQUEST))
{
Req.OnExceptionHappened += Req_OnExceptionHappened;
var response = Req.Send();
return response.ResponseHTML;
}
Just do:
using (StreamReader Reader = new StreamReader(Res.GetResponseStream()))
{
var myObject = JsonConvert.DeserializeObject<YourClass>(Reader.ReadToEnd());
}
Your code is a bit all over the place. But assuming that Res is of type WebResponse, then it should work.
So the website requires a captcha fill out in-order to login.
I'm using C# winforms and Selenium Webdrivers along with 2Captcha's API
Here's the code I got to send the request to 2Captcha's
public string SendCaptcha(string APIKey,string GoogleKey,string PageURL)
{
#region Captcha Solve
try
{
#region POST
System.Net.ServicePointManager.Expect100Continue = false;
var request = (HttpWebRequest)WebRequest.Create("http://2captcha.com/in.php");
var postData = "key="+APIKey+"&method=userrecaptcha&googlekey="+GoogleKey+"&pageurl="+PageURL;
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
#endregion
#region GET
if (responseString.Contains("OK|"))
{
string results = responseString.Replace("OK|", "");
return results;
}
else
{
return "Error";
}
#endregion
}
catch (Exception e)
{
string tt = e.Message;
return tt;
}
#endregion
}
Here's the code i got to get the solved captcha ID from 2Captcha
public string GetAnswer(string result,string APIKey)
{
#region Captcha Solve
try
{
#region POST
System.Net.ServicePointManager.Expect100Continue = false;
var request = (HttpWebRequest)WebRequest.Create("http://2captcha.com/res.php");
var postData = "key="+APIKey+"&action=get&id="+result;
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
#endregion
#region GET
if (responseString.Contains("OK|"))
{
string results = responseString.Replace("OK|", "");
return results;
}
else
{
return "Error";
}
#endregion
}
catch (Exception e)
{
string tt = e.Message;
return tt;
}
#endregion
}
And here is the login/Captcha thing
EHtvdrive.Url = "http://earnhoney.com";
EHtvdrive.FindElementByXPath("//*[#id='regorlogdiv']").Click();
Thread.Sleep(5000);
EHtvdrive.FindElementByXPath("//*[#id='email']").SendKeys(textBox4.Text);
EHtvdrive.FindElementByXPath("//*[#id='psswrd']").SendKeys(textBox3.Text);
label16.Invoke((MethodInvoker)delegate { label16.Text = "Solving Captcha."; });
label16.Invoke((MethodInvoker)delegate { label16.ForeColor = Color.Orange; });
string datakey = EHtvdrive.FindElementByXPath("//[#id='reCAPReg']").GetAttribute("data-sitekey");
string result = SendCaptcha(textBox5.Text, datakey, "http://www.earnhoney.com/en");
Thread.Sleep(60000);
string answer = GetAnswer(result, textBox5.Text);
IJavaScriptExecutor js = (IJavaScriptExecutor)EHtvdrive;
string title = (string)js.ExecuteScript("document.getElementById('g-recaptcha-response').innerHTML='" + answer + "';");
//MessageBox.Show(answer);
Thread.Sleep(3000);
EHtvdrive.FindElementById("loginbtn").Click();
Thread.Sleep(3000);
So the problem is that it gives me the Error return on the GetAnswer. Any help here? I know this is pretty complicated but really could use some help on this.
Here is my proxy code.
[Runtime.InteropServices.DllImport("wininet.dll", SetLastError = true)]
private static bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
}
private void UseProxy(string strProxy)
{
const int INTERNET_OPTION_PROXY = 38;
const int INTERNET_OPEN_TYPE_PROXY = 3;
Struct_INTERNET_PROXY_INFO struct_IPI = default(Struct_INTERNET_PROXY_INFO);
struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI));
}
private void Button1_Click(System.Object sender, System.EventArgs e)
{
Label4.Text = (TextBox1.Text + ":" + TextBox2.Text);
}
This code works fine with a windows from app. I tried using it in a console app and checked my ip but it did not work. Here is how i used it in a console app,
static void Main()
{
Console.WriteLine("Ip before Proxy /r/n");
HTTPGet req = new HTTPGet();
req.Request("http://checkip.dyndns.org");
string[] a = req.ResponseBody.Split(':');
string a2 = a[1].Substring(1);
string[] a3 = a2.Split('<');
string a4 = a3[0];
Console.WriteLine(a4);
UseProxy("219.93.183.106:8080");
Console.WriteLine("Ip after Proxy /r/n");
HTTPGet req1 = new HTTPGet();
req1.Request("http://checkip.dyndns.org");
string[] a1 = req1.ResponseBody.Split(':');
string a21 = a1[1].Substring(1);
string[] a31 = a21.Split('<');
string a41 = a31[0];
Console.WriteLine(a41);
Console.ReadLine();
}
HTTPGet is a class i got from : Get public/external IP address?
I want the proxy to work with the console app. I am not sure what is the problem
I will also run multiple instances of program so I want each console using one proxy and it should only affect the browsing of the console and not whole computer.
Proxies will have no authentication.
Here is my own HTTPBase class which handles all the http requests for the whole app,
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Web;
using System.Runtime.InteropServices;
public class HTTPBase
{
private CookieContainer _cookies = new CookieContainer();
private string _lasturl;
private int _retries = 3;
private string _useragent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; FunWebProducts; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; InfoPath.1)";
public HTTPBase()
{
ServicePointManager.UseNagleAlgorithm = false;
ServicePointManager.Expect100Continue = false;
}
public void ClearCookies()
{
this._cookies = new CookieContainer();
}
public static string encode(string str)
{
// return System.Net.WebUtility.HtmlEncode(str);
return HttpUtility.UrlEncode(str);
//return str;
}
public string get(string url)
{
for (int i = 0; i < this._retries; i++)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = this._useragent;
request.CookieContainer = this._cookies;
if (this._lasturl != null)
{
request.Referer = this._lasturl;
}
else
{
request.Referer = url;
}
this._lasturl = url;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
catch (Exception exception)
{
if ((i + 1) == this._retries)
{
throw exception;
}
}
}
throw new Exception("Failed");
}
public string post(string url, string postdata)
{
for (int i = 0; i < this._retries; i++)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
if (this._lasturl != null)
{
request.Referer = this._lasturl;
}
else
{
request.Referer = url;
}
this._lasturl = url;
request.Method = "POST";
request.UserAgent = this._useragent;
request.CookieContainer = this._cookies;
request.ContentLength = postdata.Length;
using (Stream stream = request.GetRequestStream())
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(postdata);
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream2 = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream2))
{
return reader.ReadToEnd();
}
}
}
catch (Exception exception)
{
if (this._lasturl.Contains("youtuberender.php"))
{
return "bypassing youtube error";
}
if ((i + 1) == this._retries)
{
throw exception;
}
}
}
throw new Exception("Failed");
}
public string post(string url, string postdata, string referer)
{
for (int i = 0; i < this._retries; i++)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
this._lasturl = url;
request.Method = "POST";
request.UserAgent = this._useragent;
request.CookieContainer = this._cookies;
request.ContentLength = postdata.Length;
using (Stream stream = request.GetRequestStream())
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(postdata);
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream2 = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream2))
{
return reader.ReadToEnd();
}
}
}
catch (Exception exception)
{
if (this._lasturl.Contains("youtube.com"))
{
return "bypassing youtube error";
}
if ((i + 1) == this._retries)
{
throw exception;
}
}
}
throw new Exception("Failed");
}
public string XmlHttpRequest(string urlString, string xmlContent)
{
string str = null;
HttpWebRequest request = null;
HttpWebResponse response = null;
request = (HttpWebRequest)WebRequest.Create(urlString);
try
{
byte[] bytes = Encoding.ASCII.GetBytes(xmlContent);
request.Method = "POST";
request.UserAgent = this._useragent;
request.CookieContainer = this._cookies;
request.ContentLength = bytes.Length;
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
using (Stream stream = request.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream stream2 = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream2))
{
str = reader.ReadToEnd();
}
}
}
response.Close();
}
catch (WebException exception)
{
throw new Exception(exception.Message);
}
catch (Exception exception2)
{
throw new Exception(exception2.Message);
}
finally
{
response.Close();
response = null;
request = null;
}
return str;
}
public string UserAgent
{
get
{
return this._useragent;
}
set
{
this._useragent = value;
}
}
}
}
So I want to be able to add the proxy in this code. I will pass a variable from main form which will have the proxy:port format and I want this code to use the proxy. I am new to these proxy things and having some confusions.
I use net framework 4.0 in VS 12.
You can use WebRequest.DefaultWebProxy
WebRequest.DefaultWebProxy = null;
using (WebClient wc = new WebClient())
{
string html = wc.DownloadString("http://checkip.dyndns.org");
Console.WriteLine(XDocument.Parse(html).Root.Element("body").Value);
}
WebRequest.DefaultWebProxy = new WebProxy("219.93.183.106", 8080);
using (WebClient wc = new WebClient())
{
string html = wc.DownloadString("http://checkip.dyndns.org");
Console.WriteLine(XDocument.Parse(html).Root.Element("body").Value);
}
Or you can set proxy explicitly:
using (WebClient wc = new WebClient { Proxy = null })
{
string html = wc.DownloadString("http://checkip.dyndns.org");
Console.WriteLine(XDocument.Parse(html).Root.Element("body").Value);
}
using (WebClient wc = new WebClient { Proxy = new WebProxy("219.93.183.106", 8080) })
{
string html = wc.DownloadString("http://checkip.dyndns.org");
Console.WriteLine(XDocument.Parse(html).Root.Element("body").Value);
}