Diskuze: Vyplnění formuláře
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.

Člen

Zobrazeno 8 zpráv z 8.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Jestli jsem to dobře pochopil, chtěl bys z desktop app vyplňovat údaje na
nějaké cizí web stránce?
EDIT: Nevím jestli to půjde, ale musel bys minimálně znát názvy
jednotlivých textboxů, abys věděl co se kam má vyplnit...
Máš 2 možnosti:
1. dáš tam nějaký interní webbrowser a pole vyplníš nasměrováním na odkaz
javascript:$('#field1').val('value');document.forms[0].submit();
2. použiješ webclient
Co se týče webrequestu...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Windows;
using CsQuery;
using System.Net.Http;
namespace Browser
{
class BrowserCs
{
public HttpWebRequest Request { protected set; get; }
public HttpWebResponse Response { protected set; get; }
public CookieContainer Cookies { protected set; get; }
public string DocText { protected set; get; }
public string Navigate(string url, Dictionary<string, string> args, string method = "GET", Dictionary<string, string> cookies = null)
{
if (Cookies == null)
Cookies = new CookieContainer();
Request = (HttpWebRequest)WebRequest.Create(url);
Request.Method = method;
Request.AllowAutoRedirect = true;
string data = "";
if (args.Count > 0)
{
data = (method == "GET" ? "?" : "");
for (int i = 0; i < args.Count; i++)
{
data += WebUtility.UrlEncode(args.ElementAt(i).Key) + "=" + WebUtility.UrlEncode(args.ElementAt(i).Value) + ((i == (args.Count - 1) ? "" : "&"));
//data += args.ElementAt(i).Key + "=" + args.ElementAt(i).Value + ((i == (args.Count - 1) ? "" : "&"));
}
}
if (cookies != null)
{
try
{
foreach (KeyValuePair<string, string> k in cookies)
{
string domainTemp = url.Replace("http://", "");
string domain = "";
for (int i = 0; i < domainTemp.Length; i++)
{
if (domainTemp[i] == '/')
break;
else
domain += domainTemp[i];
}
Cookie cookie = new Cookie(k.Key, k.Value, "/", domain);
Cookies.Add(cookie);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
Request.CookieContainer = new CookieContainer();
foreach (Cookie c in Cookies.GetCookies(new Uri(url)))
{
Request.CookieContainer.Add(c);
}
if (args.Count > 0)
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
Request.ContentType = "application/x-www-form-urlencoded";
Request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
Request.ContentLength = bytes.Length;
Stream stream = Request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
else
{
Request.ContentType = "text/html; charset=utf-8";
Request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
}
Response = (HttpWebResponse)Request.GetResponse();
Stream stream1 = Response.GetResponseStream();
StreamReader reader = new StreamReader(stream1);
DocText = reader.ReadToEnd();
Cookies = Request.CookieContainer;
reader.Close();
stream1.Close();
Response.Close();
return DocText;
}
public string Navigate(string url)
{
return Navigate(url, new Dictionary<string, string>());
}
public void ClearCookies()
{
Cookies = new CookieContainer();
}
}
}
Nebo také:
webBrowser1.Document.GetElementById("id text inputu").SetAttribute("value",text)
Není můj, chtěl bych to kvůli přihlašování do školní sítě. Když zapneš ntb, otevřeš prohlížeč, tak prvně tě to přesměruje na stránku, kde máš 1 jediný formulář, kam vyplníš svoje přihlašovací údaje, a poté už můžeš brouzdat dle libosti. Rád bych si udělal itilitu, která mi otevře prohlížeč a rovnou napíše a potvrdí ten formulář, abych se zbytečně nezdržoval.
Ano, pochopil jsi to správně . Však názvy textboxu najdu ve zdrojovém kódu stránky, pokuď
vím.
Zobrazeno 8 zpráv z 8.