I am a novice in programming. I have a project where I have to get data from a website (I ll be posting the website below). But it happens that I first have to select a date and press a 'Go' button and then click on another Button/Link 'View in Excel' to download this data. I cannot find a way to first click the 'Go' button after selecting my required date and then click the second button 'View in Excel' through my c# code.
Any help will be appreciated.
PS: You can check the link for yourselves http://www.mcxindia.com/sitepages/BhavCopyDateWiseArchive.aspx
private void Form1_Load(object sender, EventArgs e)
{
WebClient webClient = new WebClient();
byte[] b = webClient.DownloadData("http://www.mcxindia.com/sitepages/BhavCopyDatewise.aspx");
string s = System.Text.Encoding.UTF8.GetString(b);
var __EVENTVALIDATION = ExtractVariable(s, "__EVENTVALIDATION");
//__EVENTVALIDATION.Dump();
var forms = new NameValueCollection();
forms["__EVENTTARGET"] = "btnLink_Excel";
forms["__EVENTARGUMENT"] = "";
forms["__VIEWSTATE"] = ExtractVariable(s, "__VIEWSTATE");
forms["mTbdate"] = "01%2F15%2F2013";
forms["__EVENTVALIDATION"] = __EVENTVALIDATION;
webClient.Headers.Set(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
var responseData = webClient.UploadValues(#"http://www.mcxindia.com/sitepages/BhavCopyDatewise.aspx", "POST", forms);
System.IO.File.WriteAllBytes(#"c:\11152011.csv", responseData);
}
private static string ExtractVariable(string s, string valueName)
{
string tokenStart = valueName + "\" value=\"";
string tokenEnd = "\" />";
int start = s.IndexOf(tokenStart) + tokenStart.Length;
int length = s.IndexOf(tokenEnd, start) - start;
return s.Substring(start, length);
}
The above code gives me the file only for the last available date. I am not able to get it for the date I want.
Seems like you need to made a HTTP Post request with one post data (date). This should be easy enough. Have a look at this question and see if it helps you. Send HTTP Post Request through ASP.net
I found a solution to this question.
pls see here
Related
So i want to display [NOT FOUND] if the web request doesnt find the url specified with strings above.
What I have done is a HWID system to identify the current user. it combines 2 strings to find my github repository and in that repository a file titled with their hwid and it displays their user name inside.
I want to make it so that if it does not find that file/website url/git repository that it displays Not Found.
Everything was defined before hand / everything works how it should but if it does not find the
url it will just crash.
or if it gets removed.
also this will happen if it does not find a connection to the internet.
but i have a fix for that which will switch the text to Not Connected when the check for Online/Offline status comes back as Offline.
i have read some on else statements and as far as i know it needs an if statement above.
i dont have one there because my code did not require one before.
Can someone please help me rewrite it?
code:
private void Form1_Load(object sender, EventArgs e)
{
HWID = System.Security.Principal.WindowsIdentity.GetCurrent().User.Value;
textBox1.Text = HWID;
//downloads the username of the user
WebClient client = new WebClient();
string GithubRepository = "INSERT GITHUB LINK";
string GithubRepositoryImg = "INSERT OTHER GITHUB LINK";
string urlEndInPNG = ".png";
String strPageCode = client.DownloadString(GithubRepository+=HWID);
string strProfPicUrl = GithubRepositoryImg += HWID += urlEndInPNG;
usrNameLabel.Text = strPageCode;
// Insert else or if statement that says it to display "[NOT FOUND]" when it doesnt find it.
//my try
else{
usrNameLabel.Text = "Not Found";
}
What it displays when it finds the url
Image of what it displays
I have googled how to create one but it does not work pls help.
thank you
I think you need a try-catch more than an if/else statement.
Try as following:
private void Form1_Load(object sender, EventArgs e)
{
HWID = System.Security.Principal.WindowsIdentity.GetCurrent().User.Value;
textBox1.Text = HWID;
//downloads the username of the user
WebClient client = new WebClient();
string GithubRepository = "INSERT GITHUB LINK";
string GithubRepositoryImg = "INSERT OTHER GITHUB LINK";
string urlEndInPNG = ".png";
string strPageCode = string.Empty;
try
{
strPageCode = client.DownloadString(GithubRepository += HWID);
}
catch (Exception ex)
{
usrNameLabel.Text = "Not Found";
}
string strProfPicUrl = GithubRepositoryImg += HWID += urlEndInPNG;
usrNameLabel.Text = string.IsNullOrEmpty(strPageCode) ? usrNameLabel.Text : strPageCode;
}
When the code throws the exception, label's text will be set to "Not Found", in case it doesn't find the repository you're searching for.
Is there a way to get the whole Facebook friends list using an api!
I've tried a lot of thing and here's my shot:
FacebookClient f = new FacebookClient(access_token);
f.IsSecureConnection = true;
dynamic friendlist = await f.GetTaskAsync(#"https://graph.facebook.com/me/friendlists?access_token="+access_token);
t.Text = JsonConvert.SerializeObject(friendlist);
But all I got is an empty data!
Can anyone help me?
The friendlists endpoint is deprecated, as you can see in the breaking changes log: https://developers.facebook.com/docs/graph-api/changelog/breaking-changes#tagged-users-4-4
It would not have been what you expected anyway, it was only for lists, not friends directly. Access to all friends is not possible since a very long time. You can only get data of users/friends who authorized your App. You can use the /me/friends endpoint for that.
Another thread about getting all friends: Facebook Graph API v2.0+ - /me/friends returns empty, or only friends who also use my application
There is another way can give you an access to your all friend list names by downloading a copy of your facebook information data by selecting friends and following checkbox and wait till your file is ready then download it.
This is not the API way but for starters who want one time download list of friends
Go to the friend list page: https://www.facebook.com/friends/list
Scroll all the way down so that all friend list loads
Press F12 to open developer tools, click on console tab
A. Show in console
copy paste following script in console and hit enter.
var accumulated = "";
for (var el of document.querySelectorAll('[data-visualcompletion="ignore-dynamic"]')) {
var name = el.getAttribute("aria-label");
if(name!= null && name != "null"){
accumulated = "Name:"+name +", "+ accumulated;
console.log(accumulated);
accumulated = "";
}else{
var a = el.getElementsByTagName("a")[0];
if(a){
accumulated += "Profile URL: "+ a.getAttribute("href");
//console.log(a);
}
}
}
B. Download a .json file
copy paste following script in console and hit enter.
var exportObj = [];
var accumulated = "";
for (var el of document.querySelectorAll('[data-visualcompletion="ignore-dynamic"]')) {
var name = el.getAttribute("aria-label");
if(name!= null && name != "null"){
exportObj.push({name: name, profileURL: accumulated});
accumulated = "";
}else{
var a = el.getElementsByTagName("a")[0];
if(a){
accumulated += a.getAttribute("href");
}
}
}
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", "friendsList.json");
document.body.appendChild(downloadAnchorNode);
downloadAnchorNode.click();
downloadAnchorNode.remove();
Note: pseudo code tested in firefox
I have a project where I need to automatically download and process an excel file from a public web site.
the site is the following:
http://apps.ahca.myflorida.com/dm_web/(S(rhlzd0ac2qwvvccbyp3lx2or))/doc_results_fo.aspx
you can see a link called Export Results which downloads the excel file. This link does a postback and I have been looking all over the place to find a way to automate it without success.
This is the last code I tried:
try
{
byte[] b = webClient.DownloadData("http://apps.ahca.myflorida.com/dm_web/(S(eha2oijpqo5mro1aywok4lly))/doc_results_fo.aspx");
string s = System.Text.Encoding.UTF8.GetString(b);
var __EVENTVALIDATION = ExtractVariable(s, "__EVENTVALIDATION");
var forms = new NameValueCollection();
forms["__EVENTTARGET"] = "lbtSpreadsheet";
forms["__EVENTARGUMENT"] = "";
forms["__VIEWSTATE"] = ExtractVariable(s, "__VIEWSTATE");
forms["mTbdate"] = "11%2F15%2F2011";
forms["__EVENTVALIDATION"] = __EVENTVALIDATION;
webClient.Headers.Set(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
var responseData = webClient.UploadValues(#"http://apps.ahca.myflorida.com/dm_web/(S(eha2oijpqo5mro1aywok4lly))/doc_results_fo.aspx", "POST", forms);
System.IO.File.WriteAllBytes(#"c:\tmp\FLORIDA.xls", responseData);
}
catch (Exception ex)
{
Console.Write(ex.StackTrace);
}
}
private static string ExtractVariable(string s, string valueName)
{
string tokenStart = valueName + "\" value=\"";
string tokenEnd = "\" />";
int start = s.IndexOf(tokenStart) + tokenStart.Length;
int length = s.IndexOf(tokenEnd, start) - start;
return s.Substring(start, length);
}
This is supposed to get the value of view state and other fields and issue a POST , but when I run it the file that gets downloaded is the page itself and not the excel file.
I am not sure if this is possible using WebClient, or I should use WebBrowser control (or similar controls), or maybe an atomated browsing tool that I can record a sequence of steps and run it every x days.
Any help will be greatly appreciated.
Thank you
I figured it out using Selenium .NET
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.SetPreference("browser.download.folderList", 2);
firefoxProfile.SetPreference("browser.download.dir", strFullpath);
firefoxProfile.SetPreference("browser.helperApps.neverAsk.openFile", "application/vnd.ms-Excel");
firefoxProfile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-Excel");
IWebDriver driver = new FirefoxDriver(firefoxProfile);
driver.Navigate().GoToUrl(link);
driver.FindElement(By.Id(lookup)).Click();
driver.Quit();
I tried to make a simple app just for giving link to it and it download the direct link through an address and I have to put the name file with format in the directory address so I want something that make it changeable by the user. Any suggestions?
private void button1_Click(object sender, EventArgs e)
{
string Link;
Link = textBox1.Text;
if (!Directory.Exists(#"C:\Downloaded_Data"))
{
Directory.CreateDirectory(#"C:\Downloaded_Data");
}
else
{
}
string Dir;
Dir = #"C:\Downloaded_Data\ + txtName + txtFormat";
WebClient wc = new WebClient();
wc.DownloadFile(Link, Dir);
SoundPlayer Play = new SoundPlayer(#"E:\SteamLibrary\SteamApps\common\GarrysMod\garrysmod\sound\thrusters\jet01.wav");
Play.Play();
}
So I want that txtName and txtFormat can be changed in the textboxes you know I want them to add to the directory but I don't know how! Maybe becasue I just started :)
See http://msdn.microsoft.com/en-us/library/system.io.packaging.package.aspx There is an API which allows a Stream to be created in such a way that it may have a URI specified which points directly to said stream.
So in short if you have an array or a list and you want to provide a URI to point to the data which can be consumed by a 'Stream' then this class will help.
using c# .net4.0
I am aware the asp.net button with in the gridview of type link does a post to the same page when clicked, i need make several manipualtion on server side before actually redirecting user to an external site hence i can't use Hyperlinkfield. What i need now is the external site htm page should open up in sperate window. I tried the following which works but source site's fonts get bigger???
heres what i tried
Response.Write("<script>");
Response.Write("window.open('http://www.google.co.uk','_blank')");
Response.Write("</script>");
Response.End();
may be i need a refresh source site??
Thanks
# Curt Here is the code for Hyperlink i tired
on page load added new button on gridview
HyperLinkField LinksBoundField = new HyperLinkField();
string[] dataNavigateUrlFields = {"link"};
LinksBoundField.DataTextField = "link";
LinksBoundField.DataNavigateUrlFields = dataNavigateUrlFields;
LinksBoundField.DataNavigateUrlFormatString = "http://" + Helper.IP + "/" + Helper.SiteName + "/" + Helper.ThirdPartyAccess + "?dispage={0}&token=" + Session["Token"];
LinksBoundField.HeaderText = "Link";
LinksBoundField.Target = "_blank";
GridViewLinkedService.Columns.Add(LinksBoundField);
GridViewLinkedService.RowDataBound += new GridViewRowEventHandler(grdView_RowDataBound);
to append external values (refe and appid) to navigate url
protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
string strvalue = "";
string strvalue1 = "";
string strRef = "";
string strAppId = "";
foreach (GridViewRow row in GridViewLinkedService.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
//reference and appid
strAppId = row.Cells[0].Text;
strRef = row.Cells[1].Text;
HyperLink grdviewLink = (HyperLink)row.Cells[5].Controls[0];
strvalue = grdviewLink.NavigateUrl;
strvalue1 = Regex.Replace(strvalue, "(.*dispage\\=).*/(services.*)", "$1$2");
grdviewLink.NavigateUrl = "~/My Service/FillerPage.aspx?nurl=" + strvalue1 + "&AppID=" + strAppId.ToString() + "&Ref=" + strRef.ToString();
}
}
}
public partial class FillerPage : System.Web.UI.Page
{
private string refno = null;
private string appid = null;
private string nurl = null;
private string strvalue1 = "";
private string newtoken = "";
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString.GetValues("AppID") != null)
{
appid = Request.QueryString.GetValues("AppID")[0].ToString();
}
if (Request.QueryString.GetValues("Ref") != null)
{
refno = Request.QueryString.GetValues("Ref")[0].ToString();
}
if (Request.QueryString.GetValues("nurl") != null)
{
nurl = Request.QueryString.GetValues("nurl")[0].ToString();
}
while receiving the long url it gets messed up(same query multiple times and all jumbled up)?????
is there a better way to pass parameters ???
you need to register script not response.write
so the code for you is :
ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "<script language=JavaScript>window.open('http://www.google.co.uk','_blank')</script>");
Read more : ClientScriptManager.RegisterStartupScript.
In a situation where I need to run server side code, before then opening a new page, I sometimes create a Generic Handler File and link to this with a HyperLink, passing variables as Query Strings. Therefore something like:
/MyGenericFile.ashx?id=123
In this file, I would have some scripting that needs to be carried out, followed by a Response.Redirect().
As long as the HyperLink is set to target="_blank", the user won't even know they've been to a generic file, which is then redirected. It will appear as they've opened a new link.
Therefore the process would be:
User clicks link to .ashx file
Link opens in new window
Necessary scripting is ran
Response.Redirect() is ran
User is taken to web page (www.google.com in your example)
I believe this same process is used by advert management systems to help track clicks.
You can register a script to run on page load with ClientScriptManager.RegisterStartupScript.