I have no experience with webbrowser control I'm trying to do the following:
Navigate to website and to scrape the cookie values it gives back (specific with regex). I tried to search for this here & google there is no information about this, Are the webbrowser control connected to IE? This will be impossible to read the values right? Any way to capture the cookies? Like CookieContainer?
I have one code i use with httpwebrequest it can be an example to what I'm trying to do with webbrowser control:
List<string> cookieValues = new List<string>();
foreach (Cookie cookie in agent.LastResponse.Cookies)
{
cookieValues.Add(cookie.Value);
}
foreach (string i in cookieValues)
{
Match match2 = Regex.Match(i, #"bert=([\w]*)",
RegexOptions.IgnoreCase);
// Ensure match
if (match2.Success)
{
// Finally, we get Group value and display it.
key2 = match2.Groups[1].Value;
}
Thanks for the help like always!
Related
I am trying to post on my Facebook page using c# and Facebook sdk. I am able to post the contents on my Facebook page but it is not properly visible to others. Even the admin of the page can view only the heading of the post but can not see the picture or the content of that post. But the admin of the page can see the contents of the post by clicking on ">" this sign beside post to page. Please guide me why this is happening and how can I resolve this.
protected void Page_Load(object sender, EventArgs e)
{
CheckAuthorization();
}
private void CheckAuthorization()
{
string app_id = "My App ID";
string app_secret = "My App Secret";
string scope = "publish_stream, publish_actions";
if (Request["code"] == null)
{
Response.Redirect(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}", app_id, Request.Url.AbsoluteUri, scope));
}
else
{
Dictionary<string, string> tokens = new Dictionary<string, string>();
string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",app_id,Request.Url.AbsoluteUri,scope,Request["code"].ToString(),app_secret);
HttpWebRequest request = System.Net.WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string vals = reader.ReadToEnd();
foreach (string token in vals.Split('&'))
{
tokens.Add(token.Substring(0, token.IndexOf("=")), token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
}
}
string access_token = tokens["access_token"];
var client = new FacebookClient(access_token);
dynamic parameters = new ExpandoObject();
parameters.message = "Hello This Is My First Post To Facebook";
parameters.link = "http://www.google.com";
parameters.picture = "http://www.mywebsite.com/picture/welcome.jpg";
parameters.name = "Hello Users We welcome you all";
parameters.caption = "Posted By Ashish";
client.Post("/My Facebook Page ID/feed", parameters);
}
}
Please guide me where I am doing wrong since I am trying this for the first time after following certain posts on several websites.
You are using the user access token to post a feed to the page. So, the feeds are posted on behalf the user (not the page), which are visible as summary on the timeline.
If you post the feed on behalf of page admin itself (so that the post is visible normally on the timeline), you need to use the page access token with publish_actions permissions.
And to get the page access token you need to query: /<page-id>?fields=access_token using the normal user token with manage_pages permission.
I just want to add that sometimes your app is in MODE : developer so everything you do is visible only to the developers, it is actually the problem i had, So you need to go to your application Dashboard and look for something like "Review" [i hate giving exact paths because Facebook changes everything every once in a while], and look for something like : Your app is in development and unavailable to the public, put it to public
I am using one Image on my html page. where i am adding one anchor to that image to give the link.
that anchor contains href as ~/LB/lct.aspx?pid=177&cat=Happily In Love
Main Thing This URL IS COMING FROM DATABASE. Manually i am not entering it..
So its a Invalid URL Because of spaces between Happily In Love
I used Httputility.urlencoding and decoding also........but the problem i am facing is that..
Url is endoded properly but while i am clicking on the image its not redirecting to proper page because encoded url is not decoded..
How to resolve this...pls help me on this....
Here is the code
string url = "~/LB/lct.aspx?pid=177&cat=Happily In Love"; //your input
string[] arr = url.Split('?');
var nameValues = HttpUtility.ParseQueryString(arr[1]);
foreach (var n in nameValues.AllKeys)
{
nameValues.Set(n, HttpUtility.UrlEncode(nameValues[n]));
}
url = arr[0] + "?" + nameValues.ToString(); //your output
Use the following code to decode Querystring values
string cat = HttpUtility.UrlDecode(Request.QueryString["cat"].ToString());
Simple answer, but if it's just the spaces that are causing problems, consider replacing them with a + using a string replace:
string newurl = url.Replace(" ","+");
Note that this is only really safe if the spaces are restricted to the contents of a querystring.
I have an application that leverages a cookie to support a quasi-wizard (i.e. it's a set of pages that are navigated to by each other, and they must occur in a specific order, for registration).
When the Logon.aspx page is loaded - the default page - the browsers cookies look right.
There's one cookie and it has the right value. This ensures that the next page, which is an enrollment agreement, knows that it was loaded from the Logon.aspx page. However, when I get to that page the browsers cookies look much different:
Now we have two of the same cookie.
This doesn't appear to be causing any real issues - but I can't be sure it won't. So, let me show you the code I'm using to set the cookie (because maybe there's something wrong with it):
if (!this.IsPostBack)
{
Utility.HandleReferrer(Request, Response, "Logon.aspx");
Response.Cookies["lastpage"].Value = "Enroll.aspx";
}
and the HandleReferrer method looks like this:
static public void HandleReferrer(HttpRequest request, HttpResponse response, string expectedReferrer)
{
var cookie = request.Cookies["lastpage"];
if (cookie != null && cookie.Value.ToLower().Contains(expectedReferrer.ToLower()))
{
return;
}
response.Redirect("Logon.aspx");
}
So, why in the world does it duplicate this cookie? It doesn't ever seem to create more than two.
I suggest you do one of the following.
First, get the latest glimpse and try again.
If it is still showing 2 cookies with that name then get firebug and/or fiddler and look at it that way. If I had to take a guess I'd say there's either something wrong in glimpse or something wrong in how you are interpreting the results. Perhaps glimpse is showing what cookies existed before and then after the request was processed?
A third option is to simply emit the cookies collection from your own .net code. Something like:
foreach(HttpCookie cookie in request.Cookies) {
Response.Write(String.Format("{0} = {1}", cookie.Name, cookie.Value));
}
and see what happens.
I tried another approach, by creating a method that will return the latest cookie occurrence, this way I'll always get the right data.
This method expect the collection of cookies from Request, and the name of the searched cookie, and returns the latest ticket (the information that is normally encrypted)
private static FormsAuthenticationTicket GetLatestCookie(HttpCookieCollection cookies, string cookieName) {
var cookieOccurrences = new List<FormsAuthenticationTicket>();
for (int index = 0; index < cookies.Count; index++) {
if (cookies.GetKey(index) == cookieName) {
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookies[index].Value);
cookieOccurrences.Add(ticket);
}
}
DateTime oldestTime = DateTime.MinValue;
FormsAuthenticationTicket oldestTicket = null;
foreach (var formsAuthenticationTicket in cookieOccurrences) {
if (formsAuthenticationTicket.Expiration > oldestTime) {
oldestTime = formsAuthenticationTicket.Expiration;
oldestTicket = formsAuthenticationTicket;
}
}
return oldestTicket;
}
I have a search page on my .NET 3.5 Web Forms site that redirects a user to an external site based on the user's search parameters. I would redirect to: http://www.site.com/search.aspx?searchterm=Hello.
But now they are changing the site so that the search parameter is passed as a POST parameter, and not in the query string. So the page is expecting "searchterm".
So not only do I need to redirect to the external page, I have to post data to the page as well. I have no idea how to do this and I don't know where to start.
Is this something I can do in Web Forms without some glitchy workaround? Or maybe it can be done using jQuery?
Most browsers will explicitely deny this. Doing a cross server post like this would lead to security issues.
You can create simple JavaScript function for execute POST redirect to external page (dynamicaly generate and initilaze form object and submit it).
For example (values pattern: a=1&b=2&c=3 ...):
function bind(pageURL, values) {
var form=document.createElement('form');
form.action= pageURL;
form.target='_blank';
form.style.display = 'none';
form.method = 'POST';
var valuesSplit = node.get_value().toString().split("&");
for (var i = 0; i < valuesSplit.length - 1; i++) {
var p = valuesSplit[i];
var ps = p.split('=');
addParam(form, ps[0], ps[1]);
}
document.body.appendChild(form);
form.submit();
}
function addParam(form,key,value){
var input= document.createElement('input');
input.name=key;
input.value=value;
form.appendChild(input);
}
Does Bing has an option similar to Google Custom Search or Yahoo where I can use Bing to power the search results on my site?
Couple requirements:
Works with an ASP.NET site (is a .NET project)
Host the search box and results on my own website
Ability to customize the look and feel of the results to match my site (Full control is ideal but I understand it's not possible with free solutions)
I did a search for Bing custom search and found this: http://www.bing.com/siteowner/ but it's not exactly what I am looking for.
The query string Bing uses is:
http://www.bing.com/search?q=&src=IE-SearchBox&FORM=IE8SRC
(this is the template URL from the Bing search provider in IE). All you have to do is insert your search terms after the q parameter. A good way to test this is to actually execute a search and see the url in the address box of the browser:
http://www.bing.com/search?q=how+to+query+bing&src=IE-SearchBox&FORM=IE8SRC
you can drop the src and FORM parameters, Bing will just be using those for statistical purposes.
To get the results to appear in your own page, use an iframe, give it an id, and sets its src url (using javascript) to the search url you have constructed.
var frame = document.getElementById('mySearchFrame');
if (frame != null)
frame.src = 'http://www.bing.com/search?q=' + mySearchTerms;
Note that if you want to style the page then you will have to query Bing from code behind and "scrape" the results and place them into your own page. (Or you could just send the page back but modify the contents of it before doing so, but to do this will be violating Bing's terms of use - MS supply Bing for you to use for free, but it is on their terms, which means you will not be able to delete any advertising or change the look and feel of the page - there are no free rides in this world :).
You can sign up for site search and query Bing via jsonp and display results via javascript (exact code untested)
function searchDone(results) {
if(results.SearchResponse.Web.Results && results.SearchResponse.Web.Results.length > 0) {
for (var i = 0; i < results.SearchResponse.Web.Results.length; i++) {
result = results.SearchResponse.Web.Results[i];
item = document.createElement('li');
item.innerHTML = '' + AntiXssLibrary.HtmlEncode(result.Title.replace(/\uE000/g, "").replace(/\uE001/g, "")) + '' + '<blockquote>' + AntiXssLibrary.HtmlEncode(result.Description.replace(/\uE000/g, "").replace(/\uE001/g, "")) + '</blockquote>';
// append child to document somewhere
}
}
}
var serviceURI = "http://api.search.live.net/json.aspx?JsonType=callback&JsonCallback=searchDone&sources=web&Options=EnableHighlighting";
var appid = "&Appid=YOUR_BING_APP_ID";
var query = "&query=site:http://YOURDOMAIN.com/ <%=Request.Querystring["query"] %>";
var fullUri = serviceURI + appid + query;
var head = document.getElementsByTagName('head');
var script = document.createElement('script');
script.type = "text/javascript";
script.src = fullUri;
head[0].appendChild(script);