当我打开一个网站时,自动在文本框中添加文本
本文关键字:文本 添加 网站 一个 | 更新日期: 2023-09-27 18:00:39
因此,我正在字符串中,将一个信息放在一个文本框中,放在更特殊的文本框中一个站点的CNPJ中:
http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao.asp
但我不会。。。所以,我所尝试的。。。我试着把cnpj的值放在链接的最后一个,像这样:
http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao.asp?cnpj=00495835000160
但是,该网站没有在文本框上显示值。。。
我如何在网站上用cnpj值输入(在网站上的whiout数字,只是在链接中…)
而且,在C#中,我尝试过这个:
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);
}
}
}
}
已解决!!!首先添加这个cookie容器:
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;
}
}
}
然后将代码编辑为:
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();
}
}
}
就是这样!
特别感谢我的好朋友CunhaW!
用法:
首先创建一个带有PictureBox和TextBox的表单,以及类似的captcha的代理
private readonly ConsultaCNPJBroker _broker = new ConsultaCNPJBroker();
this.ImgCaptcha = new System.Windows.Forms.PictureBox();
this.TbxCaptcha = new System.Windows.Forms.TextBox();
然后使用类似两步查询,首先获得Captcha,然后使用Captcha 进行查询
private void UpdateCaptcha()
{
ImgCaptcha.Image = _broker.GetCaptcha();
TbxCaptcha.Text = string.Empty;
}
这里需要用户交互来解决captcha并填充文本框,最后是
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
}
也许这对你有帮助,它还不起作用。。。但几乎到了
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();
}
}
}
您必须确保目标站点支持您尝试执行的请求类型。
否则,你就倒霉了。请记住,网络应用程序的实现是制定规则的人,而不是你。
此外,看起来你正试图从巴西政府网站获取信息。通常,这类网站有一些机制来阻止你试图实现的目标,比如captchas和域验证。
好消息是,政府网站通常也会发布有关如何访问公共网络服务的规则和手册。但是,您通常也需要获得许可。
页面使用帧:
<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">
main框架包含页面cnpjreva_solicitacao2.asp
,因此调用http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao2.asp?cnpj=00495835000160(请参见页面文件名中的2)将向您显示主框架的内容,并按参数传递的方式填充CNPJ。
我说,与其手工制作生成的代码,您可以注入javascript。
例如。打开您发布的页面http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao.asp
打开JS控制台并键入以下内容:
document.getElementsByName("main")[0].contentDocument.getElementsByName("cnpj")[0].value = "MyNewValue"
请注意,表单位于frame
元素中,因此需要先获取框架元素,然后再获取输入元素。
您可以通过Web浏览器API执行同样的操作。
祝您好运,解决CAPTCHA