I need have some site manipulation from C# programming language (Microsoft Visual Studio 2010). It is site "http://m.vk.com" . I authorized on this site. It was everything normal. First web-page after authorization it was my profile page. But when I passed to another page this site I loosed my authorization. Another site page was displayed but in non authorization mode. I saved and wrote cookies files to another query. I write next my C# programming code:
I used these directives:
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 HtmlAgilityPack;
It is main function code which started after main button pushing:
private void button1_Click(object sender, EventArgs e)
{
string login = textBox1.Text;
string pass = textBox2.Text;
bool avt = http_auth_vk(login, pass);
if (avt == true)
{
toolStripStatusLabel1.Text = "Succes authorization !";
}
else
{
toolStripStatusLabel1.Text = "Authorization data incorrect !";
}
}
This is the function "http_auth_vk" :
public bool http_auth_vk(string login, string pass)
{
//*****************************
//Получаем action_url
//*****************************
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
System.Net.WebRequest reqGET = System.Net.WebRequest.Create("http://m.vk.com/");
System.Net.WebResponse resp = reqGET.GetResponse();
System.IO.Stream stream = resp.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(stream);
string s = sr.ReadToEnd();
//*****************************
//Парсим
//*****************************
// Создаём экземпляр класса
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
// Загружаем в класс (парсер) наш html
doc.LoadHtml(s);
// Извлекаем значения
HtmlNode bodyNode = doc.DocumentNode.SelectSingleNode("//div[#class='cont']/form");
// Выводим на экран значиение атрибута src
// у изображения, которое находилось
// в теге <div> в слассом bla
string result1 = bodyNode.Attributes["action"].Value;
//*****************************
//POST запрос
//*****************************
var cookies = new CookieContainer();
ServicePointManager.Expect100Continue = false;
var request = (HttpWebRequest)WebRequest.Create(result1);
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var requestStream = request.GetRequestStream())
using (var writer = new StreamWriter(requestStream))
{
writer.Write("email=" + login + "&pass=" + pass);
}
using (var responseStream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(responseStream))
{
var result = reader.ReadToEnd();
//*****************************
//Парсим, поиск ID
//*****************************
HtmlAgilityPack.HtmlDocument doc2 = new HtmlAgilityPack.HtmlDocument();
doc2.LoadHtml(result);
string result2;
try
{
//textBox3.Text = result;
HtmlNode bodyNode2 = doc2.DocumentNode.SelectSingleNode("//div[#class='user_wrap']/a");
result2 = bodyNode2.Attributes["href"].Value.Substring(3);
//Если ID найден, то авторизация удалась
//ПЕРЕХОД НА СТРАНИЦУ ГРУППЫ
//string sLocation = myHttpWebResponse.Headers["Location"];
// получам cookie
string sCookies = "";
if (!String.IsNullOrEmpty(resp.Headers["Set-Cookie"]))
{
sCookies = resp.Headers["Set-Cookie"];
}
MessageBox.Show(sCookies);
// формируем запрос
HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://m.vk.com/id100669061");
//myHttpWebRequest.Proxy = new WebProxy("127.0.0.1", 8888);
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MyIE2;";
myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
myHttpWebRequest.Headers.Add("Accept-Language", "ru");
myHttpWebRequest.ContentType = "text/plain";
if (!String.IsNullOrEmpty(sCookies))
{
myHttpWebRequest.Headers.Add(HttpRequestHeader.Cookie, sCookies);
}
// выполняем запрос
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
StreamReader myStreamReader = new StreamReader(myHttpWebResponse.GetResponseStream(), Encoding.GetEncoding(1251));
textBox3.Text = myStreamReader.ReadToEnd();
MessageBox.Show(myStreamReader.ReadToEnd());
//Console.WriteLine(myStreamReader.ReadToEnd());
//Console.ReadKey();
return true;
}
catch
{
//textBox3.Text = result;
//Если ID не науден, то авторизация не удалась
MessageBox.Show("Authorization error !");
return false;
}
}
}
How I can save authorization, after next page pass?
After a little investigation I believe I found your problem. The .NET framework will take care the cookies for you, you don't need to worry about setting the cookie or save the cookie for later use, but you do need to use the same CookieContainer for all requests.
So first set the cookie container you would like to use:
var cookies = new CookieContainer();
You can remove this, as well, all piece of code that uses sCookies.
string sCookies = "";
if (!String.IsNullOrEmpty(resp.Headers["Set-Cookie"]))
{
sCookies = resp.Headers["Set-Cookie"];
}
MessageBox.Show(sCookies);
And in the second/third request instead of having this:
if (!String.IsNullOrEmpty(sCookies))
{
myHttpWebRequest.Headers.Add(HttpRequestHeader.Cookie, sCookies);
}
You need to have this (CookieContainer)
myHttpWebRequest.CookieContainer = cookies;
Related
I have a project containing data scraping process from a password protected website. The data scraping process is set to a quartz-scheduler job, which is triggered once an hour. When I deploy the project to web, the process works fine but after 1 hour, the next trigger time, it cannot be executed.
I am sure quartz-scheduler usage is true, because a have another job done with no problem. I think problem is about socket issues which I am not good at.
Here is the code:
using Quartz;
using SMSGonder;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace TMiddaa.Quartz
{
public class Scout : IJob
{
static string baseUrl = "https://TargetWebSite.com";
static string email = "xx#yy.com";
static string password = "12345";
static string requesttype = "login";
static CookieContainer cookieContainer;
public void Execute(IJobExecutionContext context)
{
cookieContainer = new CookieContainer();
MakeWebRequest();
requesttype = "download";
MakeWebRequest();
}
public void MakeWebRequest()
{
StringBuilder postData = new StringBuilder();
string url = "";
string method = "GET";
if (requesttype == "login")
{
postData.Append(String.Format("user={0}&", email));
postData.Append(String.Format("password={0}&", password));
postData.Append(String.Format("type=&"));
postData.Append(String.Format("remember=1&"));
postData.Append(String.Format("captcha="));
method = "POST";
url = "/ajax/login.ajax.php";
}
else if (requesttype == "download")
{
url = "/somePage";
}
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData.ToString());
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseUrl + url);
request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
request.Method = method;
if (method == "POST")
{
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
}
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
request.CookieContainer = cookieContainer;
if (method == "POST")
{
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
postStream.Flush();
postStream.Close();
}
else if (method == "GET")
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//sayfanın htmlini responseString'ten alabilirsiniz.
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
//Do stuff with responseString
}
}
public int m_LastBindPortUsed = 5000;
public IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
int port = Interlocked.Increment(ref m_LastBindPortUsed); //increment
Interlocked.CompareExchange(ref m_LastBindPortUsed, 5001, 65534);
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetwork)
{
return new IPEndPoint(IPAddress.Any, port);
}
else
{
return new IPEndPoint(IPAddress.IPv6Any, port);
}
}
}
}
I get help to create this code, so I dont know some part of this code very well. I suspect the socket part. I will be very happy if someone can help.
Quartz has a logger included. You just need to plug to it to retrieve any exception that might occur in your code
If you're using .Net Core, head to https://www.quartz-scheduler.net/documentation/quartz-3.x/quick-start.html
I'm trying to login to www.autoscout24.de and retrieve adds and messages. Login form has a random generated hidden input/token. Being new to C#, I've read different tuts about using C# to login to websites and all I found was simple codes that work only in simple login forms (user:pass). I've imagined a 2-step approach: first make a GET request to retrieve needed data and a POST request with login credentials and other needed imputes. Using HtmlAgilityPack I'm passed first step but the second request just returns the login page again instead of "My account" page.
My code:
using System;
using System.IO;
using System.Net;
using System.Text;
namespace WebRequest__custom
{
class Program
{
static void Main(string[] args)
{
CookieContainer _cookies;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://angebot.autoscout24.de/login?fromNavi=myAS24");
WebResponse _response = request.GetResponse();
Stream stream = _response.GetResponseStream();
StreamReader strReader = new StreamReader(stream);
string _cookiesHeader = _response.Headers["Set-cookie"];
_cookies = request.CookieContainer;
string _content = strReader.ReadToEnd();
//Console.WriteLine(_content.Substring(0,500));
var _dom = new HtmlAgilityPack.HtmlDocument();
_dom.LoadHtml(_content);
// Get POST link
var _postLinkNode = _dom.DocumentNode.SelectSingleNode("//*[#id='loginForm']/div[3]/form");
var postLink = _postLinkNode.Attributes["action"].Value;
//Console.WriteLine(postLink);
//get Token
var _tokenNode = _dom.DocumentNode.SelectSingleNode("//*[#id='loginForm']/div[3]/form/input");
var token = _tokenNode.Attributes["value"].Value;
//Console.WriteLine(token);
// Start login request
HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("https://accounts.autoscout24.com"+ postLink);
requestLogin.ContentType = "application/x-www-form-urlencoded";
requestLogin.Method = "POST";
requestLogin.KeepAlive = true;
requestLogin.AllowAutoRedirect = true;
string postData = "&__RequestVerificationToken=" + token;
postData += "&Username=web-cppxt#mail-tester.com";
postData += "&Password=Qwert123!";
postData += "&RememberMeCheckBox=on&RememberMe=true";
byte[] _bytes = Encoding.UTF8.GetBytes(postData);
requestLogin.ContentLength = _bytes.Length;
requestLogin.CookieContainer = _cookies;
using(Stream sr = requestLogin.GetRequestStream())
{
sr.Write(_bytes, 0, _bytes.Length);
}
WebResponse loginResponse = requestLogin.GetResponse();
StreamReader loginStreamReader = new StreamReader(loginResponse.GetResponseStream());
string secondPage = loginStreamReader.ReadToEnd();
Console.WriteLine(secondPage.Substring(0,500));
Console.ReadKey();
}
}
}
I'm trying to connect Django API with c # but when I connect I face a problem in C#.
Django here I use as a server API and C# as a client.
Errors in C# are "CSRF is missing or incorrect".
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Net.Http;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
private static string json;
public static string url { get; private set; }
static void Main(string[] args)
{
/*
try
{
CookieContainer container = new CookieContainer();
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("http://127.0.0.1/api/");
request1.Proxy = null;
request1.CookieContainer = container;
using (HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse())
{
foreach (Cookie cookie1 in response1.Cookies)
{
//Console.WriteLine(response.IsSuccessStatusCode);
var csrf = cookie1.Value;
Console.WriteLine(csrf);
Console.WriteLine("name=" + cookie1.Name);
Console.WriteLine();
Console.Write((int)response1.StatusCode);
//PostRespone("http://localhost/api/multiplyfunction/");
}
}
}
catch (WebException e)
{
Console.WriteLine(e.Status);
}
/* HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/api/");
request.CookieContainer = Cookie; // use the global cookie variable
string postData = "100";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
WebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();*/
WebClient csrfmiddlewaretoken = new WebClient();
CookieContainer cookieJar = new CookieContainer();
var request1 = (HttpWebRequest)HttpWebRequest.Create("http://localhost/api/");
request1.CookieContainer = cookieJar;
var response1 = request1.GetResponse();
string baseSiteString = csrfmiddlewaretoken.DownloadString("http://localhost/api/");
// string csrfToken = Regex.Match(baseSiteString, "<meta name=\"csrf-token\" content=\"(.*?)\" />").Groups[1].Value;
// wc.Headers.Add("X-CSRF-Token", csrfToken);
csrfmiddlewaretoken.Headers.Add("X-Requested-With", "XMLHttpRequest");
using (HttpWebResponse response2 = (HttpWebResponse)request1.GetResponse())
{
foreach (Cookie cookie1 in response2.Cookies)
{
//string cookie = csrfmiddlewaretoken.ResponseHeaders[HttpResponseHeader.SetCookie];//(response as HttpWebResponse).Headers[HttpResponseHeader.SetCookie];
Console.WriteLine(baseSiteString);
//Console.WriteLine("CSRF Token: {0}", csrfToken);
//Console.WriteLine("Cookie: {0}", cookie);
//
csrfmiddlewaretoken.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8");
//wc.Headers.Add(HttpRequestHeader.Accept, "application/json, text/javascript, */*; q=0.01");
var csrf1 = cookie1.Value;
string ARG1 = ("100");
string ARG2 = ("5");
string ARG = ("ARG");
string csrfmiddlewaretoken1 =cookie1.Name +"="+cookie1.Value;
csrfmiddlewaretoken.Headers.Add(HttpRequestHeader.Cookie, csrfmiddlewaretoken1);
//string csrf = string.Join(cookie, ARG1, ARG2);
//string dataString =cookie;
// string dataString = #"{""user"":{""email"":"""+uEmail+#""",""password"":"""+uPassword+#"""}}";
string dataString = "{\"ARG1\": \"100\", \"ARG2\": \"5\"}";
// string dataString = "csrftokenmiddlewaretoken="+csrfmiddlewaretoken;
//dataString += "&ARG1=10";
//dataString += "&ARG2=10";
byte[] dataBytes = Encoding.ASCII.GetBytes(dataString);
//byte[] respon2 = csrfmiddlewaretoken.DownloadData(new Uri("http://localhost/api/statusfunction/"));
WebRequest request = WebRequest.Create("http://localhost/api/statusfunction/?ARG");
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
byte[] responseBytes = csrfmiddlewaretoken.UploadData(new Uri("http://localhost/api/multiplyfunction/"), "POST", dataBytes);
string responseString = Encoding.UTF8.GetString(responseBytes);
Console.WriteLine(responseString);
// byte[] res = csrfmiddlewaretoken.UploadFile("http://localhost/api/multiplyfunction/", #"ARG1:100");
// Console.WriteLine(result);
Console.WriteLine("value=" + cookie1.Value);
Console.WriteLine("name=" + cookie1.Name);
Console.WriteLine();
Console.Write((int)response2.StatusCode);
// PostRespone("http://localhost/api/multiplyfunction/");
}
}
}
}
}
Do you accept POST requests in your Django view? If yes, you should use #csrf_exempt decorator for the view that handles POST requests. The other option is to provide CSRF token from C# side.
The following code should log user in, but it does not work. The code uses 9gag as an example, but the general idea should work elsewhere too. The user does not get access his/hers profile.
What is wrong with the code?
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace ttp_B
{
internal class Program
{
private static void Main(string[] args)
{
bool flag = false;
//string word_to_stop = "profile";
string urltosite = "https://9gag.com/login"; // site that i'm trying to log in
string emailaddress = "";
string password = "";
var coo = new System.Net.CookieContainer();
try
{
var request = System.Net.WebRequest.Create(urltosite) as System.Net.HttpWebRequest;
request.CookieContainer = coo;
request.Method = "POST";
request.Proxy = new WebProxy("127.0.0.1", 8888); // fiddler
//some extra headers
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0";
using (var stream = request.GetRequestStream())
{
byte[] buffer =
Encoding.UTF8.GetBytes(
string.Format(
"csrftoken=&next=http%3A%2F%2F9gag.com%2F&location=1&username={0}&password={1}", emailaddress, password));
//this text of request is correct I guess. Got it from fiddler btw.
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}
using (var response = request.GetResponse() as HttpWebResponse)
{
coo.Add(response.Cookies); // adding cookies, just to make this working properly
using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
{
string http_code = sr.ReadToEnd(); // gettin' that new html document
//flag = (http_code.Contains(word_to_stop)); // looking for word that I'm sure exist after succesfull loggin' in
//if(flag == true)
//{
// console.writeline("Works");
//}
}
}
}
catch (WebException e)
{
Console.Write(e.ToString());
}
}
}
}
As far as I can see, the request stream isn't closed before you go for the response.
simplified it should look like this:
var stream = request.GetRequestStream();
tStream(buffer, 0, buffer.Length);
//close the stream
tStream.Close();
//go for the response
request.GetResponse();
So, i'm tring to put one information on one textbox, in more special textbox, CNPJ of one site:
http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao.asp
but I'm not getting... so, what I have tried... I have tried to put the cnpj value on the final of link, like this:
http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao.asp?cnpj=00495835000160
but, the site no put the value on the text box...
How I can make to enter with with the cnpj value on the site ( whiout digit on the site, just in the link... )
And, In C# I have tried this:
using System;
using System.IO;
using System.Net;
using System.Text;
namespace ReceitaFederal
{
class Program
{
static void Main(string[] args)
{
try
{
WebRequest request = WebRequest.Create("http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao.asp");
string strPost = "cnpj=00495835000160";
request.Method = "POST";
request.ContentLength = strPost.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(strPost);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
Console.WriteLine(responseFromServer);
}
catch (Exception exec)
{
Console.WriteLine(exec.GetType() + "" + exec.Message);
}
}
}
}
Solved!!! First add this cookie container:
using System;
using System.Net;
namespace ConsultaCNPJ
{
public class CookieAwareWebClient : WebClient
{
private CookieContainer _mContainer;
public void SetCookieContainer(CookieContainer container)
{
_mContainer = container;
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
var webRequest = request as HttpWebRequest;
if (webRequest != null)
{
webRequest.CookieContainer = _mContainer;
webRequest.KeepAlive = true;
webRequest.ProtocolVersion = HttpVersion.Version10;
}
return request;
}
}
}
Then edit you code to this:
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsultaCNPJ
{
public class ConsultaCNPJBroker
{
private readonly CookieContainer _cookies = new CookieContainer();
public String DominioReceitaFederal = "http://www.receita.fazenda.gov.br";
public String GetDataReceitaFederal = "/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao2.asp";
public String PostDataReceitaFederal = "/pessoajuridica/cnpj/cnpjreva/valida.asp";
private String _viewState;
public Bitmap GetCaptcha()
{
const string strViewState = "<input type=hidden id=viewstate name=viewstate value='";
const string strImagemCaptcha = "<img border='0' id='imgcaptcha' alt='Imagem com os caracteres anti robô' src='";
String htmlResult;
using (var wc = new CookieAwareWebClient())
{
wc.SetCookieContainer(_cookies);
wc.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; Synapse)";
wc.Headers[HttpRequestHeader.KeepAlive] = "300";
htmlResult = wc.DownloadString(DominioReceitaFederal + GetDataReceitaFederal);
}
if (htmlResult.Length > 0)
{
_viewState = htmlResult;
int posString = _viewState.IndexOf(strViewState, StringComparison.Ordinal);
_viewState = _viewState.Substring(posString + strViewState.Length);
posString = _viewState.IndexOf("'>", StringComparison.Ordinal);
_viewState = _viewState.Substring(0, posString);
String urlImagemCaptcha = htmlResult;
posString = urlImagemCaptcha.IndexOf(strImagemCaptcha, StringComparison.Ordinal);
urlImagemCaptcha = urlImagemCaptcha.Substring(posString + strImagemCaptcha.Length);
posString = urlImagemCaptcha.IndexOf("'>", StringComparison.Ordinal);
urlImagemCaptcha = urlImagemCaptcha.Substring(0, posString);
urlImagemCaptcha = urlImagemCaptcha.Replace("amp;", "");
if (urlImagemCaptcha.Length > 0)
{
var wc2 = new CookieAwareWebClient();
wc2.SetCookieContainer(_cookies);
wc2.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; Synapse)";
wc2.Headers[HttpRequestHeader.KeepAlive] = "300";
byte[] data = wc2.DownloadData(DominioReceitaFederal + urlImagemCaptcha);
return new Bitmap(
new MemoryStream(data));
}
return null;
}
_viewState = "";
return null;
}
public Stream Consulta(string aCNPJ, string aCaptcha, bool removerEspacosDuplos)
{
var request = (HttpWebRequest) WebRequest.Create(DominioReceitaFederal + PostDataReceitaFederal);
request.ProtocolVersion = HttpVersion.Version10;
request.CookieContainer = _cookies;
request.Method = "POST";
string fviewstate = _viewState;
fviewstate = Uri.EscapeDataString((fviewstate));
string postData = "";
postData = postData + "origem=comprovante&";
postData = postData + "viewstate=" + fviewstate + "&";
postData = postData + "cnpj=" + new Regex(#"[^\d]").Replace(aCNPJ, string.Empty) + "&";
postData = postData + "captcha=" + aCaptcha + "&";
postData = postData + "captchaAudio=&";
postData = postData + "submit1=Consultar&";
postData = postData + "search_type=cnpj";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
return response.GetResponseStream();
}
}
}
That's it!
Special thanks to my great friend CunhaW!
Usage:
First create a form with a PictureBox and a TextBox and the broker for captcha like this
private readonly ConsultaCNPJBroker _broker = new ConsultaCNPJBroker();
this.ImgCaptcha = new System.Windows.Forms.PictureBox();
this.TbxCaptcha = new System.Windows.Forms.TextBox();
Then use like 2-Steps query, first get the Captcha then do the Query with the captcha
private void UpdateCaptcha()
{
ImgCaptcha.Image = _broker.GetCaptcha();
TbxCaptcha.Text = string.Empty;
}
A user interaction is need here to solve the captcha and fill the textbox and finally
private void BtnExecute_OnClick(object sender, EventArgs e)
{
var pessoaJuridica = _broker.Consulta(TbxCNPJ.Text, TbxCaptcha.Text, true);
// here you can see props like pessoaJuridica.CNPJ
}
Maybe this help you, it's not working yet... but almost there...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Windows.Forms;
namespace LeituraWeb
{
public partial class Form1 : Form
{
String viewState;
public String Dominio_ReceitaFederal = "http://www.receita.fazenda.gov.br/";
public String GetData_ReceitaFederal = "pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao2.asp";
public String PostData_ReceitaFederal = "pessoajuridica/cnpj/cnpjreva/valida.asp";
public Form1()
{
InitializeComponent();
}
private void btnGo_Click(object sender, EventArgs e)
{
int PosString;
String StrViewState = "<input type=hidden id=viewstate name=viewstate value='";
String StrImagemCaptcha = "<img border='0' id='imgcaptcha' alt='Imagem com os caracteres anti robô' src='";
String UrlImagemCaptcha = "";
String HtmlResult = "";
using (WebClient wc = new WebClient()){
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
HtmlResult = wc.DownloadString(Dominio_ReceitaFederal + GetData_ReceitaFederal);
}
if (HtmlResult.Length > 0)
{
viewState = HtmlResult;
// executando um crop do viewstate para utilizar no post
PosString = viewState.IndexOf(StrViewState);
viewState = viewState.Substring(PosString + StrViewState.Length);
PosString = viewState.IndexOf("'>");
viewState = viewState.Substring(0, PosString);
//executando um crop na url da imagem
UrlImagemCaptcha = HtmlResult;
PosString = UrlImagemCaptcha.IndexOf(StrImagemCaptcha);
UrlImagemCaptcha = UrlImagemCaptcha.Substring(PosString + StrImagemCaptcha.Length);
PosString = UrlImagemCaptcha.IndexOf("'>");
UrlImagemCaptcha = UrlImagemCaptcha.Substring(0, PosString);
UrlImagemCaptcha = UrlImagemCaptcha.Replace("amp;", "");
// populando a imagem do captcha dentro de um picturebox
if (UrlImagemCaptcha.Length > 0)
pictureBox1.Image = new System.Drawing.Bitmap(new System.IO.MemoryStream(new System.Net.WebClient().DownloadData(Dominio_ReceitaFederal + UrlImagemCaptcha)));
}
else
{
viewState = "";
}
}
private void button1_Click(object sender, EventArgs e)
{
WebRequest request = WebRequest.Create(Dominio_ReceitaFederal + PostData_ReceitaFederal);
// Formatando o ViewState Recebido
string fviewstate = viewState;
fviewstate = System.Uri.EscapeDataString(System.Uri.UnescapeDataString(fviewstate));
// inserindo os campos a serem postados
string postData = "";
postData = postData + "origem=comprovante&";
postData = postData + "viewstate=" + fviewstate + "&";
postData = postData + "cnpj=00000000000000&";
postData = postData + "captcha=000000&";
postData = postData + "captchaAudio=&";
postData = postData + "submit1=Consultar&";
postData = postData + "search_type=cnpj";
// montando a requisicao
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
// ---------- erro aqui !!!
// retorno da sefaz ---- sempre retorna -- Parametros Inválidos
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
}
}
}
You have to make sure the target site supports the kind of request you are trying to do.
If not, you are out of luck. Keep in mind that the web app implementation is the one who dictates the rules, not you.
Additionally, it looks like you are trying to obtain information from a Brazilian government site. Usually, this type of sites have mechanisms to prevent what you are trying to achieve, such as captchas and domain validation.
The good news is that, also usually, government sites publish rules and manuals on how to access public webservices. However, you also usually need permission for that.
The page uses frames:
<frame name="top" scrolling="no" frameborder="0" marginheight="0" noresize="true" target="contents" src="cabecalho.htm" marginwidth="0" marginheight="0">
<frame frameborder="0" name="main" SRC="cnpjreva_solicitacao2.asp" scrolling="auto" noresize="false" marginheight="0" marginwidth="0">
The main frame contains the page cnpjreva_solicitacao2.asp, so calling http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao2.asp?cnpj=00495835000160 (see the 2 in the page file name) will show you the content of the main frame with the CNPJ filled as passed by the parameter.
I say instead of crafting the resulting code,
you could inject javascript.
e.g.
open the page you posted
http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao.asp
open JS Console and type the following:
document.getElementsByName("main")[0].contentDocument.getElementsByName("cnpj")[0].value = "MyNewValue"
Notice, the form is within a frame element, so you need to get the frame element and then the input element.
You can do the same via the Web Browser API.
Good Luck with solving the CAPTCHA