Post to Facebook Page from ASP.NET Website - c#

How to post something on a facebook page's wall from a asp.net website.
Google search says that I have to create an app to do this. Is it possible to post to facebook wall without creating an app?
not sure where to start. I tried developers.facebook.com and csharpsdk.org. Not much luck. Is there an example? or a tutorial using the graph api?

Creating an APP on facebook is a minimum requirement for FB to accept your posts. What you are asking doesnt make much sense. Imagine if anyone can post stuff to FB, we probably wouldnt be discussing about this here (it would have been dead by now and I am glad it is how it is) I suggest you read these links for a quick start
FB API in general:
http://developers.facebook.com/docs/reference/javascript/FB.api/
C# SDK: http://developers.facebook.com/blog/post/395/
Graph Api: http://developers.facebook.com/docs/reference/api/
Hope this gives some starting points. Good luck

After you create the application stuff in facebook and get your unique application id and secret code use the following (code behind)
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Web.Security;
using System.Xml;
using System.Collections.Specialized;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
readonly static string myurl = "your url";
readonly static string App_ID = "Your application id";
readonly static string App_Secret = "your secret code";
readonly static string scope = "email";
FacebookUser me;
readonly static DataContractJsonSerializer userSerializer = new DataContractJsonSerializer(typeof(FacebookUser));
protected void Page_Load(object sender, EventArgs e)
{
if (Session["code"] == null)
{
fbLogin.Visible = true;
}
else
{
fbLogin.Visible = false;
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if ((!IsPostBack) || Session["code"] == null)
{
string code = Request.QueryString["code"];
if (code == null)
{
Session["code"] = null;
}
else
{
Session["code"] = code;
// oAuth = new oAuthFacebook();
//// oAuthFacebook oFB = new oAuthFacebook();
// //Response.Redirect(oFB.AuthorizationLinkGet());
// oAuth.AccessTokenGet(Request["code"]);
// if (oAuth.Token.Length > 0)
// {
// //We now have the credentials, so we can start making API calls
// string url = "https://graph.facebook.com/me/likes?access_token=" + oAuth.Token;
// string json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty);
// }
}
string error_description = Request.QueryString["error_description"];
string originalReturnUrl = Request.QueryString["ReturnUrl"]; // asp.net logon param
Uri backHereUri = Request.Url; // modify for dev server
if (!string.IsNullOrEmpty(code)) // user is authenticated
{
me = GetUserDetails(code, backHereUri);
FormsAuthentication.SetAuthCookie(me.email, false); // authorize!
if (!string.IsNullOrEmpty(originalReturnUrl))
Response.Redirect(originalReturnUrl);
}
if (!string.IsNullOrEmpty(error_description)) // user propably disallowed
{
OnError(error_description);
}
fbLogin.Visible = !User.Identity.IsAuthenticated;
if (string.IsNullOrEmpty(code))
{
fbLogin.Visible = true;
LoginName1.Visible = false;
}
else
{
fbLogin.Visible = false;
LoginName1.Visible = true;
LoginName1.Text = "User Email: " + me.email.ToString() + " Name: " + me.first_name.ToString() + " Sname: " + me.last_name.ToString() + " Verified: " + me.verified.ToString();
}
fbLogin.NavigateUrl = string.Format(
CultureInfo.InvariantCulture,
"https://www.facebook.com/dialog/oauth?"
+ "client_id={0}&scope={1}&redirect_uri={2}",
App_ID,
scope,
Uri.EscapeDataString(backHereUri.OriginalString));
}
}
FacebookUser GetUserDetails(string code, Uri backHereUri)
{
Uri getTokenUri = new Uri(
string.Format(
CultureInfo.InvariantCulture,
"https://graph.facebook.com/oauth/access_token?"
+ "client_id={0}&client_secret={1}&code={2}&redirect_uri={3}",
App_ID,
App_Secret,
Uri.EscapeDataString(code),
Uri.EscapeDataString(backHereUri.OriginalString))
);
using (var wc = new WebClient())
{
string token = wc.DownloadString(getTokenUri);
Session["token"] = token;
Uri getMeUri = new Uri(
string.Format(
CultureInfo.InvariantCulture,
"https://graph.facebook.com/me?{0}",
token)
);
using (var ms = new MemoryStream(wc.DownloadData(getMeUri)))
{
var me = (FacebookUser)userSerializer.ReadObject(ms);
return me;
}
}
}
void OnError(string error_description)
{
Controls.Add(new Label()
{
CssClass = "oauth-error",
Text = error_description
}
);
}
[Serializable]
class FacebookUser
{
public long id;
public string name;
public string first_name;
public string last_name;
public string link;
public string email;
public string timezone;
public string locale;
public bool verified;
public string updated_time;
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
WebRequest req = WebRequest.Create("https://api.facebook.com/restserver.php?method=links.getStats&urls=http://localhost:5064/default.aspx,http://www.facebook.com");
WebResponse resp = req.GetResponse();
StreamReader reader = new StreamReader(resp.GetResponseStream());
string xml = reader.ReadToEnd();
xml = xml.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").Replace("links_getStats_response xmlns=\"http://api.facebook.com/1.0/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd\" list=\"true\"", "links_getStats_response");
doc.LoadXml(xml);
XmlNodeList xnList = doc.SelectNodes("links_getStats_response/link_stat");
foreach (XmlNode xn in xnList)
{
string fullurls = xn["url"].InnerText;
string likes = xn["like_count"].InnerText;
string comment = xn["comment_count"].InnerText;
string normalisedurls = xn["normalized_url"].InnerText;
}
}
public string WebRequests(string method, string url, string postData)
{
HttpWebRequest webRequest = null;
StreamWriter requestWriter = null;
string responseData = "";
webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = method.ToString();
webRequest.ServicePoint.Expect100Continue = false;
webRequest.UserAgent = HttpContext.Current.Request.UserAgent;
webRequest.Timeout = 20000;
if (method == "POST")
{
webRequest.ContentType = "application/x-www-form-urlencoded";
//POST the data.
requestWriter = new StreamWriter(webRequest.GetRequestStream());
try
{
requestWriter.Write(postData);
}
catch
{
throw;
}
finally
{
requestWriter.Close();
requestWriter = null;
}
}
responseData = WebResponseGet(webRequest);
webRequest = null;
return responseData;
}
public string WebResponseGet(HttpWebRequest webRequest)
{
StreamReader responseReader = null;
string responseData = "";
try
{
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
responseData = responseReader.ReadToEnd();
}
catch
{
throw;
}
finally
{
webRequest.GetResponse().GetResponseStream().Close();
responseReader.Close();
responseReader = null;
}
return responseData;
}
protected void Button2_Click(object sender, EventArgs e)
{
if (Session["code"] != null)
{
string urls = "https://graph.facebook.com/me/feed?" + Session["token"];
var json = WebRequests("POST", urls, "message=" + HttpUtility.UrlEncode("" + TextBox1.Text));
}
}
}
}
AND for default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HyperLink runat="server" Text="Log in with Facebook" id="fbLogin"/><br /><br />
<asp:Label ID="LoginName1" runat="server" Text="" Font-Bold="true"></asp:Label>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Get Likes And Comments" onclick="Button1_Click" />
<br />
<asp:TextBox ID="TextBox1" runat="server" Width="407px" Text="Testing Api by c# code"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Post To me"
onclick="Button2_Click" />
</div>
</form>
</body>
</html>
Hope that helps

Related

implementing Invisible Google reCaptcha for asp.net application ?

This is my ASP.NET form. I want to add invisible recaptcha to it with server side validation. Can someone please help?
I can do client side validation but it doesnt use secret key. My another questions is Do we need secret key for invisible recaptcha?
Please see serverside code that i used for google recaptcha but it is not working for Invisible recaptcha. I am getting this error : -
reCAPTCHA Error: missing-input-response: Not Valid Recaptcha
<div id="ContactFormDiv" runat="server">
<div class="form-row form-required">
<asp:Label ID="YourNameLabel" runat="server" AssociatedControlID="YourNameTextBox"> Your Name:</asp:Label>
<asp:TextBox ID="YourNameTextBox" runat="server" CssClass="form300" MaxLength="150"></asp:TextBox>
</div>
<div class="form-row form-required">
<div id='recaptcha' class="g-recaptcha"
data-sitekey="site key"
data-callback="onSubmit"
data-size="invisible">
</div>
</div>
<div class="form-row-buttons">
<asp:Button ID="SendMessageButton" ClientIDMode="Static" runat="server" Text="Send Message" CssClass="buttonPositive"
CausesValidation="True" OnClick="SendMessageButton_Click" />
</div>
</div>
Javascript Code
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js" async defer></script>
Serverside Code
public class MyObject
{
public string success { get; set; }
}
public static string ReCaptcha_Key = "------------------Site Key-----------------";
public static string ReCaptcha_Secret = "--------------Secret Key ---------------";
public bool ValidateReCaptcha()
{
bool Valid = false;
//start building recaptch api call
var sb = new StringBuilder();
//Getting Response String Append to Post Method
string Response = Request["g-recaptcha-response"];
string url = "https://www.google.com/recaptcha/api/siteverify?secret=" + ReCaptcha_Secret + "&response=" + Response;
sb.Append(url);
//make the api call and determine validity
using (var client = new WebClient())
{
var uri = sb.ToString();
var json = client.DownloadString(uri);
var serializer = new DataContractJsonSerializer(typeof(RecaptchaApiResponse));
var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
var result = serializer.ReadObject(ms) as RecaptchaApiResponse;
//--- Check if we are able to call api or not.
if (result == null)
{
lblmsg.Text = "Captcha was unable to make the api call";
}
else // If Yes
{
//api call contains errors
if (result.ErrorCodes != null)
{
if (result.ErrorCodes.Count > 0)
{
foreach (var error in result.ErrorCodes)
{
lblmsg.Text = "reCAPTCHA Error: " + error;
}
}
}
else //api does not contain errors
{
if (!result.Success) //captcha was unsuccessful for some reason
{
lblmsg.Text = "Captcha did not pass, please try again.";
}
else //---- If successfully verified. Do your rest of logic.
{
lblmsg.Text = "Captcha cleared ";
Valid = true;
}
}
}
}
return Valid;
}
public bool temp = true;
protected void SendMessageButton_Click(object sender, EventArgs e)
{
temp = ValidateReCaptcha();
if (temp == false)
{
lblmsg.Text = "Not Valid Recaptcha";
lblmsg.ForeColor = System.Drawing.Color.Red;
}
else
{
lblmsg.Text = "Successful";
lblmsg.ForeColor = System.Drawing.Color.Green;
}
Page.Validate();
if (this.Page.IsValid == true && temp == true)
{ //Page and invisible recaptcha is valid }
}
I am getting this error : -
reCAPTCHA Error: missing-input-response: Not Valid Recaptcha
This is how I implemented the working sample:
-- Client Side (Refer to Google Documentation )
<head>
<!-- Google Invisible Captcha -->
<script src='https://www.google.com/recaptcha/api.js'/>
<script>
function onSubmit(token) {
document.getElementById("htmlForm").submit();
}
</script>
</head>
<body>
<form id="htmlForm" action="Default.aspx" method="post">
<input name="txtName" />
<input name="txtEmailAddress" />
<button class="g-recaptcha btn btn-default"
data-sitekey="-------------------Site key--------------"
data-callback="onSubmit">
Submit Request
</button>
</form>
</body>
-- Server Side (keeps secret Key)
public static bool IsValidCaptcha()
{
var secret = "--------------Secret Key ---------------";
var req =
(HttpWebRequest)
WebRequest.Create("https://www.google.com/recaptcha/api/siteverify?secret=" + secret + "&response=" + HttpContext.Current.Request.Form["g-recaptcha-response"]);
using (var wResponse = req.GetResponse())
{
using (StreamReader readStream = new StreamReader(wResponse.GetResponseStream()))
{
string responseFromServer = readStream.ReadToEnd();
if (!responseFromServer.Contains("\"success\": false"))
return true;
}
}
return false;
}
I also have similar problem and it looks like it is harder to find any decent example. However, I saw that you have set
data-callback="onSubmit"
but I didn't see where you have defined that method. Is it there? Could that be what are you missing?

Google Recaptcha not visible ASP.NET

I am trying to set up Google reCaptcha on ASP.NET web forms but it is not visible.
So far I took the following approach:
At the beginning of the .ascx page:
<%# Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
And the following in the form I want it to show:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<div class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>
<recaptcha:RecaptchaControl
ID="recaptcha"
runat="server"
PublicKey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"
PrivateKey="_My private key taken from the reCaptcha API_"
/>
However, when I run the page, the reCaptcha is not visible.
Any help is appreciated. Thanks
Looks like you're using the old reCaptcha.
Google deprecated it, and encourages you to switch to NoCaptcha re Captcha 2.
Here's how you do it:
page.aspx
<html>
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<form>
page.aspx.cs
public partial class page : System.Web.UI.Page
{
private string CAPTCHA_SECRET_KEY = #"6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"; // WARNING: FAKE SECRET KEY
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
String username = unameInput.Text;
String password = pwordInput.Text;
if (validate())
{
// ...
}
}
}
// Thanks to http://www.thatsoftwaredude.com/content/6235/implementing-googles-no-captcha-recaptcha-in-aspnet
private bool validate()
{
string url = #"https://www.google.com/recaptcha/api/siteverify";
WebRequest request = WebRequest.Create(url);
string postData = string.Format("secret={0}&response={1}&remoteip={2}", CAPTCHA_SECRET_KEY, Request["g-recaptcha-response"], Request.UserHostAddress);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(postData);
writer.Close();
StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream());
string responseData = reader.ReadToEnd();
JavaScriptSerializer jss = new JavaScriptSerializer();
CaptchaResponse cResponse = jss.Deserialize<CaptchaResponse>(responseData);
return cResponse.success;
}
catch(WebException)
{
return true; // TODO: Change to false when releasing
}
}
class CaptchaResponse
{
public bool success { get; set; }
}
use this. create a external class
public class Recaptcha
{
public bool ValidateCaptcha(string sitekey, string responseRecaptcha)
{
//var response = Request["g-recaptcha-response"]; //part of the parameter << you need to pass this as your responseRecaptcha
//secret that was generated in key value pair
//part of the parameter
var client = new WebClient();
var reply =
client.DownloadString(
string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}",sitekey,responseRecaptcha));
var captchaResponse = JsonConvert.DeserializeObject<CaptchaResponse>(reply);
string status = "";
//when response is false check for the error message
if (!captchaResponse.Success)
{
if (captchaResponse.ErrorCodes.Count <= 0) return true;
var error = captchaResponse.ErrorCodes[0].ToLower();
switch (error)
{
case ("missing-input-secret"):
status = "The secret parameter is missing.";
return false;
break;
case ("invalid-input-secret"):
status = "The secret parameter is invalid or malformed.";
return false;
break;
case ("missing-input-response"):
status = "The response parameter is missing.";
return false;
break;
case ("invalid-input-response"):
status = "The response parameter is invalid or malformed.";
return false;
break;
default:
status = "Error occured. Please try again";
return false;
break;
}
}
else
{
status = "Valid";
}
return true;
}
}
internal class CaptchaResponse
{
[JsonProperty("success")]
public bool Success { get; set; }
[JsonProperty("error-codes")]
public List<string> ErrorCodes { get; set; }
}

Scriptmanager equaivalent required in converting webform to mvc

Basically I have a webform with a scriptmanager and a button to update the results returned from the class webspider (web spider that searches for broken links for a given url). I am trying to convert the code to mvc and have achieved most of this except for the code that is highlighted in bold as I do not have a scriptmanager in the mvc view, can some please give me some pointers on how I can do this, many thanks. -
default.aspx -
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_check404._Default" %>
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1><%: Title %>.</h1>
<h2>Modify this template to jump-start your ASP.NET application.</h2>
</hgroup>
<p>
To learn more about ASP.NET, visit http://asp.net.
The page features <mark>videos, tutorials, and samples</mark> to help you get the most from ASP.NET.
If you have any questions about ASP.NET visit
our forums.
</p>
</div>
</section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h3>Search results:</h3>
<asp:Label ID="lblTot" runat="server" />
<asp:Label runat="server" ID="lblStatus" />
<asp:UpdatePanel runat="server" ID="pnlLinks">
<ContentTemplate> -->
<asp:GridView runat="server" ID="grvLinks" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="NavigateUrl" HeaderText="Url" />
<asp:ButtonField DataTextField="Status" HeaderText="Status" />
</Columns>
</asp:GridView>
<asp:Button runat="server" ID="btnUpdateResults" Text="Update Results" />
</ContentTemplate>
</asp:UpdatePanel>
default.aspx.cs -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _check404.Spider;
using System.Net;
namespace _check404
{
public partial class _Default : Page
{
private string script = #"setTimeout(""__doPostBack('{0}','')"", 5000);";
private WebSpider WebSpider
{
get { return (WebSpider)(Session["webSpider"] ?? (Session["webSpider"] = new WebSpider())); }
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!this.WebSpider.IsRunning)
{
string url = "http://bbc.co.uk";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
this.WebSpider.Execute(url);
lblTot.Text = WebSpider.Tot.ToString();
}
//////////////////////////the following code is what I am trying to convert
if (this.WebSpider.IsRunning)
{
this.lblStatus.Text = "Processing...";
ScriptManager.RegisterStartupScript(this, this.GetType(),
this.GetType().Name, string.Format(script, this.btnUpdateResults.ClientID), true);
}
///////////////////////////////////////////////////////////////////////////////////////
this.grvLinks.DataSource = this.WebSpider.Links;
this.grvLinks.DataBind();
}
}
}
spider.cs -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
namespace _check404.Spider
{
public class WebSpider
{
public int Tot { get; set; }
const int LIMIT = 10;
string[] invalidTypes = { ".zip", ".doc", ".css", ".pdf", ".xls", ".txt", ".js", ".ico" };
public List<Link> Links;
public bool IsRunning { get; set; }
public WebSpider()
{
this.Links = new List<Link>();
}
public void Execute(string url)
{
this.Links.Clear();
this.Links.Add(new Link() { Status = HttpStatusCode.OK, NavigateUrl = url });
this.IsRunning = true;
WaitCallback item = delegate(object state) { this.FindLinks((UrlState)state); };
ThreadPool.QueueUserWorkItem(item, new UrlState() { Url = url, Level = 0 });
Tot = Links.Count();
}
public void FindLinks(UrlState state)
{
try
{
string html = new WebClient().DownloadString(state.Url);
MatchCollection matches = Regex.Matches(html, "href[ ]*=[ ]*['|\"][^\"'\r\n]*['|\"]");
foreach (Match match in matches)
{
string value = match.Value;
value = Regex.Replace(value, "(href[ ]*=[ ]*')|(href[ ]*=[ ]*\")", string.Empty);
if (value.EndsWith("\"") || value.EndsWith("'"))
value = value.Remove(value.Length - 1, 1);
if (!Regex.Match(value, #"\((.*)\)").Success)
{
if (!value.Contains("http:"))
{
Uri baseUri = new Uri(state.Url);
Uri absoluteUri = new Uri(baseUri, value);
value = absoluteUri.ToString();
}
if (this.Links.Exists(x => x.NavigateUrl.Equals(value))) continue;
try
{
bool validLink = true;
foreach (string invalidType in invalidTypes)
{
string v = value.ToLower();
if (v.EndsWith(invalidType) || v.Contains(string.Format("{0}?", invalidType)))
{
validLink = false;
break;
}
}
if (validLink)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(value);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
this.Links.Add(new Link() { Status = response.StatusCode, NavigateUrl = value });
if (response.StatusCode == HttpStatusCode.OK && state.Level < LIMIT)
{
WaitCallback item = delegate(object s) { FindLinks((UrlState)s); };
ThreadPool.QueueUserWorkItem(
item, new UrlState() { Url = value, Level = state.Level + 1 });
}
}
}
catch
{
this.Links.Add(new Link()
{
Status = HttpStatusCode.ExpectationFailed,
NavigateUrl = value
});
}
}
}
}
catch
{
///
/// If downloading times out, just ignore...
///
}
}
}
}
It looks like you are trying to automatically click a button after 5 seconds if the your spider job is running.
All the script manager is doing is embedding a tag containing your javascript when the markup is generated.
Id say the easiest way to do this is to add an attribute to your model.
class MyModel
{
public bool SpiderRunning { get; set; }
}
Then set this in your controller:
model.SpiderRunning = this.WebSpider.IsRunning
Then in your view, only add the script to the page if this value is true:
#if(Model.SpiderRunning)
{
<script>setTimeout(function(){document.getElementById("btnUpdateResults").click();}, 5000);</script>
}

Trying to download files from a website but it does nothing and doesn't throw errors

In general the code is working when im crawling to sites like www.cnn.com or www.foxnews.com
But now i tried this site: https://github.com/jasonwupilly/Obsidian/tree/master/Obsidian
And i wanted to download files for example when its crawling there is a link like this:
https://github.com/jasonwupilly/Obsidian/blob/master/Obsidian/Obsidian.sln
So the file on my hard disk should be Obsidian.sln
This is my crawling method:
public List<string> webCrawler(string mainUrl, int levels)
{
List<string> csFiles = new List<string>();
wc = new System.Net.WebClient();
HtmlWeb hw = new HtmlWeb();
List<string> webSites;
csFiles.Add("temp string to know that something is happening in level = " + levels.ToString());
csFiles.Add("current site name in this level is : " + mainUrl);
try
{
HtmlAgilityPack.HtmlDocument doc = TimeOut.getHtmlDocumentWebClient(mainUrl, false, "", 0, "", "");
if (doc == null)
{
failed = true;
wccfg.failedUrls++;
failed = false;
}
else
{
done = true;
// progress should be reported here I guess
Object[] temp_arr = new Object[8];
temp_arr[0] = csFiles;
temp_arr[1] = mainUrl;
temp_arr[2] = levels;
temp_arr[3] = currentCrawlingSite;
temp_arr[4] = sitesToCrawl;
temp_arr[5] = done;
temp_arr[6] = wccfg.failedUrls;
temp_arr[7] = failed;
OnProgressEvent(temp_arr);
currentCrawlingSite.Add(mainUrl);
webSites = getLinks(doc);
removeDupes(webSites);
removeDuplicates(webSites, currentCrawlingSite);
removeDuplicates(webSites, sitesToCrawl);
if (wccfg.removeext == true)
{
for (int i = 0; i < webSites.Count; i++)
{
webSites.Remove(removeExternals(webSites, mainUrl, wccfg.localy));
}
}
if (wccfg.downloadcontent == true)
{
retwebcontent.retrieveImages(mainUrl);
}
if (wccfg.downloadallfiles == true)
{
retwebcontent.retrieveFiles(mainUrl);
}
if (levels > 0)
sitesToCrawl.AddRange(webSites);
webSites = FilterJunkLinks(webSites);
if (levels == 0)
{
return csFiles;
}
else
{
for (int i = 0; i < webSites.Count(); i++)//&& i < 20; i++)
{
if (wccfg.toCancel == true)
{
return new List<string>();
}
if (pause == true)
{
_busy.Reset();
}
else
{
_busy.Set();
string t = webSites[i];
if ((t.StartsWith("http://") == true) || (t.StartsWith("https://") == true))
{
csFiles.AddRange(webCrawler(t, levels - 1));
}
}
}
return csFiles;
}
}
return csFiles;
}
catch (WebException ex)
{
failed = true;
wccfg.failedUrls++;
return csFiles;
}
catch (Exception ex)
{
failed = true;
wccfg.failedUrls++;
throw;
}
}
In the beginning of this method i have this line:
HtmlAgilityPack.HtmlDocument doc = TimeOut.getHtmlDocumentWebClient(mainUrl, false, "", 0, "", "");
I used a breakpoint on this line and enter it to the class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Net;
using System.Web;
using System.Threading;
using DannyGeneral;
namespace GatherLinks
{
class TimeOut
{
static HtmlAgilityPack.HtmlDocument doc;
public TimeOut()
{
}
class MyClient : WebClient
{
public bool HeadOnly { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest req = base.GetWebRequest(address);
if (HeadOnly && req.Method == "GET")
{
req.Method = "HEAD";
}
return req;
}
}
public static HtmlAgilityPack.HtmlDocument getHtmlDocumentWebClient(string url, bool useProxy, string proxyIp, int proxyPort, string usename, string password)
{
try
{
doc = null;
using (MyClient clients = new MyClient())
{
clients.HeadOnly = true;
byte[] body = clients.DownloadData(url);
// note should be 0-length
string type = clients.ResponseHeaders["content-type"];
clients.HeadOnly = false;
// check 'tis not binary... we'll use text/, but could
// check for text/html
if (type == null)
{
return null;
}
else
{
if (type.StartsWith(#"text/html"))
{
string text = clients.DownloadString(url);
doc = new HtmlAgilityPack.HtmlDocument();
WebClient client = new WebClient();
//client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.Credentials = CredentialCache.DefaultCredentials;
client.Proxy = WebRequest.DefaultWebProxy;
if (useProxy)
{
//Proxy
if (!string.IsNullOrEmpty(proxyIp))
{
WebProxy p = new WebProxy(proxyIp, proxyPort);
if (!string.IsNullOrEmpty(usename))
{
if (password == null)
password = string.Empty;
NetworkCredential nc = new NetworkCredential(usename, password);
p.Credentials = nc;
}
}
}
doc.Load(client.OpenRead(url));
}
}
}
}
catch (Exception err)
{
}
return doc;
}
private static string GetUrl(string url)
{
string startTag = "Url: ";
string endTag = " ---";
int startTagWidth = startTag.Length;
int endTagWidth = endTag.Length;
int index = 0;
index = url.IndexOf(startTag, index);
int start = index + startTagWidth;
index = url.IndexOf(endTag, start + 1);
string g = url.Substring(start, index - start);
return g;
}
}
}
Its doing the firstl ines in getHtmlDocumentWebClient method and when its getting to the line:
byte[] body = clients.DownloadData(url);
Its jumping to the method : GetWebRequest
After doing the line: return req; it does nothing its returning to the program and nothing happen.
And thats only if the link is: https://github.com/jasonwupilly/Obsidian/tree/master/Obsidian
If the link is www.cnn.com for example its downloading images no problems and also crawling no problems.
THe code is some long but it's all connected so i had to add it all.
What could be the problem here ?

web service is not working on server side

i have created a we bservice named test.asmx which looks like this:
[WebService(Namespace = "http://insforia.com/Webservices/StockTickers/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[WebMethod(Description = "Using stock symbol gets delayed stock information from Yahoo.", EnableSession = false)]
public string[] GetQuote()
{
string username = Session["username"].ToString();
string company = "BSE-100.BO+COALINDIA.BO+TCS.BO+WIPRO.BO+SBIN.NS+MNM.BO+RCOM.BO+TATASTL.BO+GMRINFRA.BO+ICICIBANK.BO";
string stockcodes = "sl1c6";
string url = "http://in.finance.yahoo.com/d/quotes.csv?s=" + company + "&f=" + stockcodes;
var webRequest = HttpWebRequest.Create(url);
var webResponse = webRequest.GetResponse();
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
{
buffer = sr.ReadToEnd();
}
var buffer = buffer.Replace("\"", "");
buffer = buffer.Replace("\r\n", ",");
var bufferList = buffer.Split(new char[] { ',' });
return bufferList;
}
and i am calling this web service in my aspx page like this:
private string userid = "admin";
protected void Page_Load(object sender, EventArgs e)
{
Session["username"] = userid;
test oo = new test();
bufferList = oo.GetQuote();
}
the thing is its working perfectly in my localhost but when i uploaded it on server its giving me an error that it couldn't find the test
to see the exact error this is the link :
http://insforia.com/Webservices/StockTickers/stockmain.aspx
i don't understand what to do? why this is happening. i know this is a very stupid question to ask here but i am trying this from yesterday.
try to dont use session.
send param for userName;
public string[] GetQuote(string UserName)
{
...
return bufferList;
}

Categories

Resources