Asked by:
how to create bot in asp.net for login to another website and get some results in c#

Question
-
User836525179 posted
hi guys
i need to create bot for login to another url and get some request in that page in c#
this is my login bot code
Uri uri1 = new Uri("http://crm.sibesabzadsl.ir/index.php/main/login"); string strPostData = String.Format("Username={0}&Password={1}", "*******", "******"); HttpWebRequest webreq1 = WebRequest.Create(uri1) as HttpWebRequest; webreq1.Method = "post"; webreq1.ContentLength = strPostData.Length; webreq1.ContentType = "application/x-www-form-urlencoded"; CookieContainer cookieContainer = new CookieContainer(); webreq1.CookieContainer = cookieContainer; StreamWriter web1_p = new StreamWriter(webreq1.GetRequestStream()); web1_p.Write(strPostData); web1_p.Close(); HttpWebResponse Hr_resp = (HttpWebResponse)webreq1.GetResponse(); StreamReader sr_reader = new StreamReader(Hr_resp.GetResponseStream()); string weba = sr_reader.ReadToEnd(); Label1.Text = weba.ToString(); sr_reader.Close();
when this code run it show me logged in page
like below:
in this page i need open this link
http://crm.sibesabzadsl.ir/index.php/main/support
in this page i need my page auto put value in that text box in page and auto click on blue bottun to search value and return me result
of course it must then auto click on number 1 and then show me number 2 content like this picture
how i can create this bot?
Tuesday, March 31, 2020 11:15 AM
All replies
-
User283571144 posted
Hi uniservice3,
Could you please post more details information about auto put value in that text box in page and auto click on blue bottun to search value and return me result?
Do you mean if I click the support button, then it will auto type in the 1 value into the textbox? What you mean about the bot?
Best Regards,
Brando
Thursday, April 2, 2020 12:49 AM -
User836525179 posted
ty brando
my english is not well
can i have your skype id?
Thursday, April 2, 2020 9:27 AM -
User-719153870 posted
Hi uniservice3,
Seems your images are all 404, can you update them along with the description?
auto click on blue bottun to search value and return me resultit must then auto click on number 1 and then show me number 2 content like this pictureWhen you say "auto", you mean programmatically right? I think this is feasible as long as we get more detailed information about the requirement.
ps: We don't provide skype support, thanks for cooperation.
Best Regard,
Yang Shen
Friday, April 3, 2020 1:30 AM -
User836525179 posted
hi again
ty yang shen
look this picture
this is login page
i need write code than login automaticly to dashboard
this picture is dashboard page after i logged in manually
in this page i put that number on textbox like picture ( number 1 ) and when i pressed Enter bottom it show me below picture
in this page i pressed on help icon (number 3 ) then it show me that details (number 4)
this is manually
now my problem is than i need do this work programmatically from another project to get that details (number4)
my auto login code is like below but i dont know how i can do another steps (put number then press enter bottom then click on help icon then get that details)
Uri uri1 = new Uri("http://crm.sibesabzadsl.ir/index.php/main/login"); string strPostData = String.Format("Username={0}&Password={1}", "h_net2020.ir", "******"); HttpWebRequest webreq1 = WebRequest.Create(uri1) as HttpWebRequest; webreq1.Method = "post"; webreq1.ContentLength = strPostData.Length; webreq1.ContentType = "application/x-www-form-urlencoded"; CookieContainer cookieContainer = new CookieContainer(); webreq1.CookieContainer = cookieContainer; StreamWriter web1_p = new StreamWriter(webreq1.GetRequestStream()); web1_p.Write(strPostData); web1_p.Close(); HttpWebResponse Hr_resp = (HttpWebResponse)webreq1.GetResponse(); StreamReader sr_reader = new StreamReader(Hr_resp.GetResponseStream()); string weba = sr_reader.ReadToEnd(); Label1.Text = weba.ToString(); sr_reader.Close();
Friday, April 3, 2020 7:55 AM -
User-719153870 posted
Hi uniservice3,
Sorry for this late reply and thanks for the updated description, the requirement is clear now.
The point to convert from manually to automatically is, fire your functions(including search function in your second image, help button in your third image) automatically.
There are two suggestions i can provide.
- Use HttpRequest.QueryString in your page load event, check if there's parameter in your request url, call related functions if there is. For example, add your expected textbox's value as the parameter: http://xxxx.xxx?search={the value you should manually put in the textbox}, check if the Request.QueryString["search"] is null, call search funtion if it's not null.
- Add webapi to your app and let that api call the functions.
Best Regard,
Yang Shen
Monday, April 6, 2020 1:51 AM -
User836525179 posted
ty yang shen
this is support search form code and post link (second image)
<form class="sidebar-search " action="http://crm.sibesabzadsl.ir/index.php/main/support" method="POST"> <a href="javascript:;" class="remove"> <i class="icon-close"></i> </a> <div class="input-group"> <input type="text" class="form-control" name="stel" placeholder="پشتیبانی..."> <span class="input-group-btn"> <a href="javascript:;" class="btn submit"> <i class="icon-magnifier"></i> </a> </span> </div> </form>
and this is help bottun functions in third image
html code
<div class="col-xs-4 col-md-4 center"> <div class="Support-Item " onclick="Info();"> <i class="fa fa-info-circle"></i><br> <span>اطلاعات مشترک</span> </div> </div>
java script functions
function get_tel_info() { //pstninfo $('#dTelInfo').html('<img src="http://crm.sibesabzadsl.ir/assets/img/Loader.gif" />'); $.post('http://crm.sibesabzadsl.ir/index.php/main/get_tel_info', { tel: tel, owner: cdata.Owner, nonce: cdata.nonce }, function (data) { $('#pstninfo').html('<table width="100%">' + data + "</div>"); $('#pstninfo').css('display', 'table-cell'); $('#dTelInfo').html(''); $('#pstninfo').css('padding-bottom', '10px'); }).fail(function() { $('#pstninfo').html(''); }); } function Info() { if (cdata == null) return; if (cdata.Emkan == 'hatam') { $.isLoading({ text: "لطفا صبر کنید" }); $.post('http://crm.sibesabzadsl.ir/index.php/main/support', { op: 6, tel: tel }, function (data) { finded = false; $(data).find('tr').each(function(index, data) { $(data).find('td').each(function (index, data) { if (finded) { name = $(data).html(); finded = false; } if ($(data).html().trim() == 'نام مشترک') { finded = true; } }); }); cdata.Name = name; $('#name').html(cdata.Name + ' ' + cdata.TelNumber); $('#Message').html(data); $('#op').html('نتیجه اطلاعات مشترک'); UpdateTitles(); }).done(function() { $.isLoading( "hide" ); }); } else { $('#op').html('نتیجه اطلاعات مشترک'); $('#Message').html(cdata.Info); $('html, body').animate({ scrollTop: $("#Message").offset().top }, 300); UpdateTitles(); } function GetInfo() { try { tel = $('#tel').val(); if (tel.trim() == '') { showError('تلفن را وارد نمایید'); return false; } if (hide) $.isLoading({ text: "لطفا صبر کنید" }); //$.ajaxSetup({timeout:5000}); $.post('http://crm.sibesabzadsl.ir/index.php/main/get_info', { tel: tel }, function(data) { if (data == 'hatam') { cdata = { Emkan: 'hatam', TelNumber: tel }; Info(); return; } if (data.Error == 'too many') { //showError('تعداد درخواست ها زیاد می باشد. لطفا مجدد تلاش نمایید.'); setTimeout('GetInfo()', 500); hide = false; return; } hide = true; try{ /*var xmlDoc = $.parseXML(data); var $xml = $(xmlDoc); cdata = { TelNumber: $xml.find('Tel').text(), Emkan: $xml.find('Emkan').text(), Info: $xml.find('Info').text(), Name: $xml.find('Name').text(), History: $xml.find('History').text(), ID: $xml.find('ID').text(), Group: $xml.find('Group').text(), Password: $xml.find('Password').text(), Mac: $xml.find('Mac').text(), Port: $xml.find('Port').text(), };*/ if (data.Address) { p = data.Data; $.isLoading({ text: "لطفا صبر کنید" }); $.post(data.Address, { data: data.Data }, function(data) { UpdateEditInfo(data); $.post('http://crm.sibesabzadsl.ir/index.php/main/update_log', { data: p, state: data.State, oname: data.OwnerName, o: data.Owner, d: data.Days, s: data.Service, r: data.Reserve, n: data.Name, m: data.Mobile}, function() { }); }, 'json').always(function() { $.isLoading( "hide" ); }); } else { /*cdata = data; AddDataToInfo(); tel = cdata.TelNumber; // cdata = data; $('#name').html(cdata.Name + ' ' + cdata.TelNumber); $('#Message').html(cdata.Emkan);*/ UpdateEditInfo(data); } } catch (err) { } // Emkansanji(); // $('#test').html(JSON.stringify(data)); }, 'json').done(function() { } ).fail(function (arg1, arg2, arg3) { }).always(function(){ if (hide) $.isLoading( "hide" ); }); } catch (err) { } }
Monday, April 6, 2020 2:38 PM -
User-719153870 posted
Hi uniservice3,
There's another way to create this auto bot to click and input values for specific elements in your web page - the webdriver selenium c#.
Notice that in the webdriver Webelement Commands, you can use Click command to click specific elements and use SendKeys command to input values.
Go through this tutorial and you shall be able to implement the auto process.
Best Regard,
Yang Shen
Tuesday, April 7, 2020 2:27 AM