I am using a HttpWebRequest and a WebProxy to test some proxy servers to see if they are still functional or not. I set up the timeout to 1000 milliseconds but in some cases the program times out after more than 20 seconds. This happens in the catch loop. Is there a way to fix this? I don't want to wait more than 2 seconds for a proxy. I am using .Net 4.5.2
string GetPageThroughProxy(string sUrl, string sHost, string sProxyIPAddress, string sProxyPort, string sUsername, string sPassword)
{
Stopwatch stopwatch = Stopwatch.StartNew();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
HttpWebRequest httpWebRequest=(HttpWebRequest)WebRequest.Create(sUrl);
string proxyAddress = sProxyIPAddress;
string proxyPort = sProxyPort;
string username = sUsername;
string password = sPassword;
WebProxy wp = new WebProxy(proxyAddress + ":" + proxyPort);
wp.UseDefaultCredentials = false;
wp.Credentials = new NetworkCredential(username, password);
httpWebRequest.Proxy = wp;
string sReferer = sUrl;
httpWebRequest.ProtocolVersion = HttpVersion.Version11;
httpWebRequest.UserAgent = Variables.userAgent; // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 Edg/84.0.522.40";
httpWebRequest.Accept = Variables.reqAccept; // "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
httpWebRequest.Headers["Accept-Encoding"] = "gzip, deflate, br";
httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
httpWebRequest.Headers["Accept-Language"] = "en-US,en;q=0.9";
httpWebRequest.MaximumAutomaticRedirections = 2;
httpWebRequest.Method = "GET";
httpWebRequest.Referer = sReferer;
httpWebRequest.Timeout = 1000;
httpWebRequest.ReadWriteTimeout = 1000;
try {
WebResponse response = httpWebRequest.GetResponse();
string responseFromServer = string.Empty;
stopwatch.Stop();
MessageBox.Show(stopwatch.Elapsed.ToString("mm\\:ss\\.ff"));
using (Stream dataStream = response.GetResponseStream()) {
StreamReader reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
}
response.Close();
return responseFromServer;
}
catch(Exception ex) {
MessageBox.Show("Exception " + ex.Message + "\n\n" + stopwatch.Elapsed.ToString("mm\\:ss\\.ff"));
}
return string.Empty;
}
Related
I would like to get the HTML data from specific website ("https://www.justdial.com/Chennai/Silver-Oak-Service-Apartments-Next-to-Vivek-Showroom-Selaiyur/044PXX44-XX44-111215152228-W2G5_BZDET?xid=Q2hlbm5haSBIb3RlbHM=")
Interesting part is, I can able to get the data for other websites like (Google, StackOverFlow, etc)
I'm using below code, But I'm receiving "The operation has timed out" always.
public static string GetPageData(string url)
{
try
{
ServicePointManager.DefaultConnectionLimit = 7000;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36";
httpWebRequest.Headers.Add("Accept-Language", "en-US,fr-CA,it-IT;q=0.6");
httpWebRequest.ContentType = "text/html";
httpWebRequest.AllowAutoRedirect = true;
httpWebRequest.Method = "GET";
httpWebRequest.CookieContainer = new CookieContainer();
httpWebRequest.CookieContainer.Add(new Uri("https://justdial.com"), new CookieCollection());
httpWebRequest.KeepAlive = true;
httpWebRequest.MaximumAutomaticRedirections = 200;
httpWebRequest.Timeout = 7000;
using (var response = (HttpWebResponse)httpWebRequest.GetResponse())
{
Stream responseStream = response.GetResponseStream();
if (responseStream == null)
return string.Empty;
StreamReader streamReader = new StreamReader(responseStream);
string end = streamReader.ReadToEnd();
streamReader.Close();
streamReader.Dispose();
responseStream.Close();
responseStream.Dispose();
response.Close();
return end;
}
}
catch (Exception ex)
{
return ex.Message;
}
}
I want to try get current DateTime like this :
try {
HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://www.timestampconvert.com/");
req.Method = "GET";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36";
req.ContentType = "text/html; charset=utf-8";
req.Referer = string.Empty;
req.KeepAlive = true;
req.Timeout = 25000;
//req.Proxy = proxy;
HttpWebResponse res = (HttpWebResponse) req.GetResponse();
Stream Stream = res.GetResponseStream();
StreamReader reader = new StreamReader(Stream);
string reader_str = reader.ReadToEnd();
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(reader_str);
var divs = from div in htmlDoc.DocumentNode.Descendants("div")
select div;
DateTime dt = DateTime.Now;
foreach(var div in divs) {
if (div.Attributes["class"] != null) {
if (div.Attributes["class"].Value == "remarkrow") {
if (div.InnerText.Contains("Computation based on input date")) {
CultureInfo cultureinfo = new CultureInfo("en-US");
dt = Convert.ToDateTime(div.InnerText.Replace("\n\t*)Computation based on input date ", string.Empty), cultureinfo);
dt = dt.AddHours(2).AddMinutes(30);
break;
}
}
}
}
DateTime dt_ = dt;
}
catch(Exception ex) {
MessageBox.Show(ex.ToString());
}
But it has an exception like below :
The request was aborted: Could not create SSL/TLS secure channel
How can i fix this error?
I'm trying to log in at http://carkit.kg (django app) via C# with following code
HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create("http://carkit.kg");
tokenRequest.CookieContainer = new CookieContainer();
string token = "";
using (var response = (HttpWebResponse)tokenRequest.GetResponse()) {
token = response.Cookies["csrftoken"].ToString().Split('=')[1];
}
HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create("http://carkit.kg");
var cache = new CredentialCache();
cache.Add(new Uri("http://carkit.kg"), "Basic", new NetworkCredential(username, password));
loginRequest.Credentials = cache;
loginRequest.PreAuthenticate = true;
loginRequest.Method = "POST";
loginRequest.CookieContainer = new CookieContainer();
loginRequest.CookieContainer.Add(new Cookie("csrftoken", token) {Domain="carkit.kg"});
Debug.Log(token);
byte[] data = Encoding.ASCII.GetBytes("username=" + username + "&password=" + password + "&csrfmiddlewaretoken=" + token);
loginRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
loginRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
loginRequest.Headers.Add("Cache-Control", "max-age=0");
loginRequest.Headers.Add("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
//loginRequest.ContentType = "application/x-www-form-urlencoded";
loginRequest.Headers.Add("Origin", "http://carkit.kg");
loginRequest.Referer = "http://carkit.kg/";
loginRequest.Headers.Add("UpgradeInsecureRequests", "1");
loginRequest.Headers.Add("XCompress", "null");
loginRequest.Headers.Add("ContentType", "application/x-www-form-urlencoded");
loginRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
loginRequest.Headers.Add("X-CSRFToken",token);
loginRequest.ContentLength = data.Length;
loginRequest.Timeout = 3000;
loginRequest.GetRequestStream().Write(data, 0, data.Length);
loginRequest.Headers.Add("X-CSRFToken", token);
HttpWebResponse authResponse = (HttpWebResponse)loginRequest.GetResponse();
Debug.Log(authResponse.ResponseUri);
Both requests running well, but last line returns incorrect uri (if login is correct it should redirect me to /game and stay at / in other case) - anyway it returns /. Redirect is enabled and you see which headers I've included into request. What is the problem?
I've just used django-rest-framework as authentication service and LogIn method got this representation:
public void LogIn(string username, string password)
{
var request = (HttpWebRequest)WebRequest.Create("http://carkit.kg/api/v1/auth/login/");
var postData = "username=" + username;
postData += "&password=" + password;
var data = Encoding.UTF8.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);
}
try {
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
// Successful login
}
else {
serverMessenger.SendErrorMessage(0);
Debug.LogError("Cannot Find User. TryToLogin finished");
}
} catch (WebException e) {
serverMessenger.SendErrorMessage(0);
Debug.LogError("Cannot Find User. TryToLogin finished");
}
}
I'm trying to connect to the Lighthouse api using C# code. This is the php example https://support.lighthouserocks.com/hc/en-gb/articles/201319732-API-The-Basics which it describe how to do this. But I'm fails with it. I was try both NetworkCredentials & send in the Header but still have 401 Unauthorized access to it, here is the code:
public string RequestResponse()
{
HttpWebRequest webRequest = WebRequest.Create(HomeUrl) as HttpWebRequest;
webRequest.Method = "GET";
webRequest.ContentType = "application/json";
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Timeout = 20000;
string auth = CreateAuthorization("domain.lhlive.com", "user", "token");
webRequest.Headers["auth"] = "Basic " + auth;
//webRequest.Credentials = new NetworkCredential("user", "token");
//webRequest.PreAuthenticate = true;
//webRequest.Headers.Add("auth", "user, token");
webRequest.Accept = "application/vnd.lighthouse.v1.hal+json";
Stream responseStream = null;
StreamReader responseReader = null;
string responseData = "";
try
{
WebResponse webResponse = webRequest.GetResponse();
responseStream = webResponse.GetResponseStream();
responseReader = new StreamReader(responseStream);
responseData = responseReader.ReadToEnd();
}
finally
{
if (responseStream != null)
{
responseStream.Close();
responseReader.Close();
}
}
return responseData;
}
public void Test()
{
using (var client = new WebClient())
{
client.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
client.Headers["ContentType"] = "application/json";
client.Headers["Accept"] = "application/vnd.lighthouse.v1.hal+json";
//client.Headers["Lighthouse Username"] = "user";
//client.Headers["API Key"] = "token";
client.Headers["WWW-Authenticate"] = "user, token";
byte[] arr = client.DownloadData("https://domain.lhlive.com/contacts");
Console.WriteLine("--- WebClient result ---");
Console.WriteLine(arr.Length);
}
}
anybody know what I should to do?
Hard to say because I can't access Lighthouse but try the following (notice how auth header is set).
var webRequest = WebRequest.Create("http://some.endpoint.com/") as HttpWebRequest;
webRequest.Method = "GET";
webRequest.Accept = "application/vnd.lighthouse.v1.hal+json";
webRequest.ContentType = "application/json";
webRequest.Headers["Authorization"] = string.Format("Basic {0}",
Convert.ToBase64String(Encoding.Default.GetBytes(
string.Format("{0}:{1}", "your username", "your API key"))));
var response = webRequest.GetResponse();
var stream = response.GetResponseStream();
var data = (new StreamReader(stream)).ReadToEnd();
In this case your Authorization header looks like "Basic eW91ciB1c2VybmFtZTp5b3VyIEFQSSBrZXk=".
I doesn't understand how do I check login is successful or not. How can I check login success using httpwebrequest and httpresponse?
my Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Diagnostics;
namespace Way2sms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private CookieCollection cc = new CookieCollection();
private void Send_Click(object sender, EventArgs e)
{
string uid = txtUsername.Text.Trim();
string password = txtpass.Text.Trim();
string message = txtmsg.Text.Trim();
string no = txtno.Text.Trim();
try
{
WebBrowser wb = new WebBrowser();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://site5.way2sms.com/Login1.action");
req.Method = "POST";
CookieContainer con = new CookieContainer();
req.CookieContainer = con;
req.CookieContainer.Add(GetC());
req.KeepAlive = true;
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11";
req.ContentType = "application/x-www-form-urlencoded";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Referer = "http://site5.way2sms.com/content/index.html";
byte[] data = System.Text.Encoding.Default.GetBytes("username=" + uid + "&password=" + password);
req.ContentLength = data.Length;
req.AllowAutoRedirect = true;
req.ServicePoint.Expect100Continue = true;
Stream str = req.GetRequestStream();
str.Write(data, 0, data.Length);
str.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
wb.ScriptErrorsSuppressed = true;
wb.DocumentText = new StreamReader(res.GetResponseStream()).ReadToEnd();
webBrowser1.Navigate(wb.DocumentText);
HttpWebRequest X = (HttpWebRequest)WebRequest.Create("http://site5.way2sms.com/quicksms.action");
X.Method = "POST";
X.CookieContainer = con;
X.KeepAlive = true;
X.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11";
X.ContentType = "application/x-www-form-urlencoded";
X.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
X.Referer = "http://site5.way2sms.com/jsp/InstantSMS.jsp";
byte[] datax = Encoding.Default.GetBytes("HiddenAction=instantsms&catnamedis=Birthday&Action=sa65sdf656fdfd&chkall=on&MobNo=" + no + "&textArea=" + message);
X.ContentLength = datax.Length;
X.AllowAutoRedirect = true;
X.ServicePoint.Expect100Continue = true;
str = X.GetRequestStream();
str.Write(datax, 0, datax.Length);
str.Close();
HttpWebResponse resx = (HttpWebResponse)X.GetResponse();
wb.DocumentText = new StreamReader(resx.GetResponseStream()).ReadToEnd();
}
catch (Exception ex)
{
}
}
private CookieCollection GetC()
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://site5.way2sms.com/");
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(cc);
req.CookieContainer.Add(cc);
req.KeepAlive = true;
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11";
req.ContentType = "application/x-www-form-urlencoded";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Referer = "http://site5.way2sms.com/content/index.html";
req.AllowAutoRedirect = true;
req.ServicePoint.Expect100Continue = true;
return ((HttpWebResponse)req.GetResponse()).Cookies;
}
Here is my code for sending sms using way2sms. I want to check enter user name and password is correct or not? so How can I check?
Edited
In order to do what you need, you will need to understand HttpWebRequest and HttpWebResponse. Look at this article which will give you a good picture.
Using HttpWebRequest and HttpWebResponse
After that, you can search through your response by this function:
WebRequest _request = (HttpWebRequest)WebRequest.Create("UrlToGet");
using (WebResponse response = _request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string text = reader.ReadToEnd();
}
}
What you do with the "text" variable, is upto you.