Help with Janrain Engage openID C# code behind for asp.net forms website - c#

I have signed up with Janrain Engage to incorporate its free widget based openID. But i can't figure out what values to put in the C# code behind. They have provided with a C# helper class, the one below:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.XPath;
public class Rpx
{
private string apiKey;
private string baseUrl;
public Rpx(string apiKey, string baseUrl) {
while (baseUrl.EndsWith("/"))
baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
this.apiKey = apiKey;
this.baseUrl = baseUrl;
}
public string getApiKey() { return apiKey; }
public string getBaseUrl() { return baseUrl; }
public XmlElement AuthInfo(string token) {
Dictionary<string,string> query = new Dictionary<string,string>();
query.Add("token", token);
return ApiCall("auth_info", query);
}
public List<string> Mappings(string primaryKey) {
Dictionary<string,string> query = new Dictionary<string,string>();
query.Add("primaryKey", primaryKey);
XmlElement rsp = ApiCall("mappings", query);
XmlElement oids = (XmlElement)rsp.FirstChild;
List<string> result = new List<string>();
for (int i = 0; i < oids.ChildNodes.Count; i++) {
result.Add(oids.ChildNodes[i].InnerText);
}
return result;
}
public Dictionary<string,ArrayList> AllMappings() {
Dictionary<string,string> query = new Dictionary<string,string>();
XmlElement rsp = ApiCall("all_mappings", query);
Dictionary<string,ArrayList> result = new Dictionary<string,ArrayList>();
XPathNavigator nav = rsp.CreateNavigator();
XPathNodeIterator mappings = (XPathNodeIterator) nav.Evaluate("/rsp/mappings/mapping");
foreach (XPathNavigator m in mappings) {
string remote_key = GetContents("./primaryKey/text()", m);
XPathNodeIterator ident_nodes = (XPathNodeIterator) m.Evaluate("./identifiers/identifier");
ArrayList identifiers = new ArrayList();
foreach (XPathNavigator i in ident_nodes) {
identifiers.Add(i.ToString());
}
result.Add(remote_key, identifiers);
}
return result;
}
private string GetContents(string xpath_expr, XPathNavigator nav) {
XPathNodeIterator rk_nodes = (XPathNodeIterator) nav.Evaluate(xpath_expr);
while (rk_nodes.MoveNext()) {
return rk_nodes.Current.ToString();
}
return null;
}
public void Map(string identifier, string primaryKey) {
Dictionary<string,string> query = new Dictionary<string,string>();
query.Add("identifier", identifier);
query.Add("primaryKey", primaryKey);
ApiCall("map", query);
}
public void Unmap(string identifier, string primaryKey) {
Dictionary<string,string> query = new Dictionary<string,string>();
query.Add("identifier", identifier);
query.Add("primaryKey", primaryKey);
ApiCall("unmap", query);
}
private XmlElement ApiCall(string methodName, Dictionary<string,string> partialQuery) {
Dictionary<string,string> query = new Dictionary<string,string>(partialQuery);
query.Add("format", "xml");
query.Add("apiKey", apiKey);
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> e in query) {
if (sb.Length > 0) {
sb.Append('&');
}
sb.Append(System.Web.HttpUtility.UrlEncode(e.Key, Encoding.UTF8));
sb.Append('=');
sb.Append(HttpUtility.UrlEncode(e.Value, Encoding.UTF8));
}
string data = sb.ToString();
Uri url = new Uri(baseUrl + "/api/v2/" + methodName);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
// Write the request
StreamWriter stOut = new StreamWriter(request.GetRequestStream(),
Encoding.ASCII);
stOut.Write(data);
stOut.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream ();
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = false;
doc.Load(dataStream);
XmlElement resp = doc.DocumentElement;
if (resp == null || !resp.GetAttribute("stat").Equals("ok")) {
throw new Exception("Unexpected API error");
}
return resp;
}
public static void Main(string[] args) {
Rpx r = new Rpx(args[0], args[1]);
if (args[2].Equals("mappings")) {
Console.WriteLine("Mappings for " + args[3] + ":");
foreach(string s in r.Mappings(args[3])) {
Console.WriteLine(s);
}
}
if (args[2].Equals("all_mappings")) {
Console.WriteLine("All mappings:");
foreach (KeyValuePair<string, ArrayList> pair in r.AllMappings()) {
Console.WriteLine(pair.Key + ":");
foreach (string identifier in pair.Value) {
Console.WriteLine(" " + identifier);
}
}
}
if (args[2].Equals("map")) {
Console.WriteLine(args[3] + " mapped to " + args[4]);
r.Map(args[3], args[4]);
}
if (args[2].Equals("unmap")) {
Console.WriteLine(args[3] + " unmapped from " + args[4]);
r.Unmap(args[3], args[4]);
}
}
}
Forgive me but i'm not a master of C#, but i can't figure out where to put the values for the apiKey and the token_url. Also if a user signs in how to display the username as derived from the accounts he is using to sign up, Google or Yahoo! for example.
Also there is no sign out option provided.
Any help would be much appreciated as the Janrain developer help is nothing but useless.

I agree the JanRain does not make this very clear. You do not need to modify the Rpx helper example code provided. Here is some code that makes use of their Rpx helper class from an MVC application. Be sure to include System.Xml and System.Xml.Linq.
var token = Request.Form["token"];
var rpx = new Helpers.Rpx("your-api-key-goes-here", "https://rpxnow.com");
var authInfo = rpx.AuthInfo(token);
var doc = XDocument.Load(new XmlNodeReader(authInfo));
ViewData["displayName"] = doc.Root.Descendants("displayName").First().Value;
ViewData["identifier"] = doc.Root.Descendants("identifier").First().Value;

Related

Code indicating that the "_edgeKey" object is null when the code tries to access it

I have a C# code that connects to a set of APIs and retrieves and displays specific data. Initially, the code was written in a way that heavily depended on Internet Explorer (the user had to be logged in to their account only through IE; otherwise, the application couldn't make a connection to the API). Since the IE expired, the code could no longer connect to the API, so I replaced the cookies with edge cookies. I receive no errors; however, the code is not giving the desire outcome. When I debugged, it looks like the exception of "Error:_edgeKey is null" is being thrown. How can I fix this?
I have created a folder named Controllers which includes StaticStrings.cs, WebAPI.cs, WebNav.cs.
StaticStrings.cs includes the set of APIs
WebAPI.cs includes the following code:
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using WeeklyScheduleModels;
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
namespace WeeklySchedule.Contollers
{
class WebAPI
{
public string GetNorderTimeExpensebyNo(string norderNo)
{
return "OK";
}
public string GetNorderDbIDfromNo(string norderNo)
{
return "";
}
public async Task<double> GetBookedHours(string subOrderNo)
//public double GetBookedHours(string subOrderNo)
{
using (var edgeCookie = new EdgeCookie())
{
var cookies = edgeCookie.GetCookies(new Uri(StaticStrings.UrlNCert), false);
var cookieString = new StringBuilder();
foreach (var cookie in cookies)
{
cookieString.Append(cookie.Name + "=" + cookie.Value + "; ");
}
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Cookie", cookieString.ToString());
string uri = "";
double totalHours = 0.0;
uri = StaticStrings.NOrderTimeExpenseAPI + subOrderNo;
var response = await client.GetStringAsync(uri);
NCertTimeExpenseMain ntem = JsonConvert.DeserializeObject<NCertTimeExpenseMain>(response);
foreach (OracleHour oh in ntem.oracleHours)
{
totalHours += oh.hours;
}
return totalHours;
}
}
//To get the status of the project from Ncert
public async Task<string> GetOrderStatus(string subOrderNo)
{
using (var edgeCookie = new EdgeCookie())
{
var cookies = edgeCookie.GetCookies(new Uri(StaticStrings.UrlNCert), false);
var cookieString = new StringBuilder();
foreach (var cookie in cookies)
{
cookieString.Append(cookie.Name + "=" + cookie.Value + "; ");
}
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Cookie", cookieString.ToString());
string uri = "";
uri = StaticStrings.OrderStatusAPI + subOrderNo + '?';
var response = await client.GetStringAsync(uri);
RootObject status = JsonConvert.DeserializeObject<RootObject>(response);
string currentstatusofproject = status.statusDescription;
return currentstatusofproject;
}
}
public CustomerList GetCustomerListFromPartialName(string partialName)
{
using (var edgeCookie = new EdgeCookie())
{
var cookies = edgeCookie.GetCookies(new Uri(StaticStrings.UrlNCert), false);
var cookieString = new StringBuilder();
foreach (var cookie in cookies)
{
cookieString.Append(cookie.Name + "=" + cookie.Value + "; ");
}
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Cookie", cookieString.ToString());
string searchStr = StaticStrings.CustomerSearchAPI + partialName + "&%24format=json&%24top=30&%24filter=status%20eq%20%27Active%27&%24count=true";
var response = client.GetStringAsync(searchStr).Result;
CustomerList cl = JsonConvert.DeserializeObject<CustomerList>(response);
return cl;
}
}
public List<FrameAgreement> GetAgreementsFromCustomerId(string customerId)
{
using (var edgeCookie = new EdgeCookie())
{
var cookies = edgeCookie.GetCookies(new Uri(StaticStrings.UrlNCert), false);
var cookieString = new StringBuilder();
foreach (var cookie in cookies)
{
cookieString.Append(cookie.Name + "=" + cookie.Value + "; ");
}
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Cookie", cookieString.ToString());
string searchStr = StaticStrings.CustomerAgreementAPI + customerId;
var response = client.GetStringAsync(searchStr).Result;
List<FrameAgreement> FAList = JsonConvert.DeserializeObject<List<FrameAgreement>>(response);
return FAList;
}
}
public List<Address> GetAddressFromCustomerId(string customerId)
{
using (var edgeCookie = new EdgeCookie())
{
var cookies = edgeCookie.GetCookies(new Uri(StaticStrings.UrlNCert), false);
var cookieString = new StringBuilder();
foreach (var cookie in cookies)
{
cookieString.Append(cookie.Name + "=" + cookie.Value + "; ");
}
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Cookie", cookieString.ToString());
string searchStr = StaticStrings.CustomerAddressAPI + customerId + "/survey-address/all";
var response = client.GetStringAsync(searchStr).Result;
List<Address> AddressList = JsonConvert.DeserializeObject<List<Address>>(response);
return AddressList;
}
}
}
}
WebNav.cs Includes the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
namespace WeeklySchedule.Contollers
{
public class EdgeCookie : IDisposable
{
private Microsoft.Win32.RegistryKey _edgeKey = null;
public EdgeCookie()
{
_edgeKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\AppContainer\Storage\microsoft.microsoftedge_8wekyb3d8bbwe\MicrosoftEdge\Main", false);
}
public List<Cookie> GetCookies(Uri uri, bool v)
{
var cookies = new List<Cookie>();
try
{
if (_edgeKey == null)
{
throw new Exception("Error: _edgeKey is null");
}
var sqliteFile = System.IO.Path.Combine(_edgeKey.GetValue("DataPath").ToString(), "Cookies");
using (var conn = new System.Data.SQLite.SQLiteConnection("Data Source=" + sqliteFile + ";Version=3;"))
{
conn.Open();
using (var cmd = new System.Data.SQLite.SQLiteCommand("select host_key, name, encrypted_value, path, expires_utc, secure, httponly from cookies where host_key like '%" + uri.Host + "%'", conn))
{
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var cookie = new Cookie(
reader.GetString(1),
reader.GetString(2),
reader.GetString(3),
reader.GetString(0));
cookie.Expires = DateTime.FromFileTimeUtc(long.Parse(reader.GetString(4)));
cookie.Secure = reader.GetBoolean(5);
cookie.HttpOnly = reader.GetBoolean(6);
cookies.Add(cookie);
}
}
}
}
}
catch
{
throw new Exception("Error reading cookies from Microsoft Edge");
}
return cookies;
}
public void Dispose()
{
if (_edgeKey != null)
{
_edgeKey.Dispose();
_edgeKey = null;
}
}
}
}

Is there a function to skip repeating values being written into text file?

I'm trying to get all hyperlinks from an array that match the word "contacts" into .csv text file. The problem is if it finds another contact hyperlink on the same website it will print it again. How do I fix this? Also how to web scrape multiple websites for specific div that contains keywords: ("Phone number", "Address", "E-mail" and etc.)?
private void contactbutton_Click(object sender, EventArgs e)
{
ArrayList domainlist = new ArrayList();
const Int32 BufferSize = 128;
// -- Location of domain list file --
using (var fileStream = File.OpenRead("C:/Users/Username/Desktop/domains.txt"))
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
{
String line;
while ((line = streamReader.ReadLine()) != null)
domainlist.Add(line);
}
foreach (string s in domainlist)
{
SearchHyperlinks("https://" + s);
}
}
public static void SearchHyperlinks(string address4)
{
HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = hw.Load(address4);
String GetAbsoluteUrlString(string baseUrl, string url)
{
var uri = new Uri(url, UriKind.RelativeOrAbsolute);
if (!uri.IsAbsoluteUri)
uri = new Uri(new Uri(baseUrl), uri);
return uri.ToString();
}
try
{
using (var w = new StreamWriter("C:/Users/Username/Desktop/hyperlink.csv"))
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[starts-with(., 'Contacts') or starts-with(., 'contacts') or starts-with(., 'CONTACTS') or starts-with (., 'Shop Contacts')]"))
{
String hrefValue = link.Attributes["href"].Value;
if (hrefValue != null)
{
String fullhref = GetAbsoluteUrlString(address4, hrefValue);
Console.WriteLine(fullhref);
using (var textWriter = new StreamWriter("C:/Users/Username/Desktop/hyperlinks.csv", true))
{
var writer = new CsvWriter(textWriter, CultureInfo.InvariantCulture);
writer.Configuration.Delimiter = ",";
writer.WriteField(fullhref);
writer.NextRecord();
}
}
}
}
catch (System.NullReferenceException)
{
Console.WriteLine("Hyperlinks not found");
}
Rewrite your method to actually Search, and not Write:
public static IEnumerable<string> SearchHyperlinks(string address4)
{
var hw = new HtmlWeb();
var doc = hw.Load(address4);
String GetAbsoluteUrlString(string baseUrl, string url)
{
var uri = new Uri(url, UriKind.RelativeOrAbsolute);
if (!uri.IsAbsoluteUri)
uri = new Uri(new Uri(baseUrl), uri);
return uri.ToString();
}
var links = doc.DocumentNode.SelectNodes("//a[starts-with(., 'Contacts') or starts-with(., 'contacts') or starts-with(., 'CONTACTS') or starts-with (., 'Shop Contacts')]");
if(links == null)
yield return break;
foreach (var link in links)
{
var hrefValue = link.Attributes["href"].Value;
if (hrefValue != null)
{
var fullhref = GetAbsoluteUrlString(address4, hrefValue);
yield return fullhref;
}
}
}
Then you distinct values returned:
var distinct = SearchHyperlinks(input).Distinct();
Then you write them all wherever you want.

Unity C# GraphQL Post Request Body Authentication

I am working on trying to connect to my GraphQL server my developers have setup from within Unity. I have found some scripts to help with this process, however, I am still not able to connect because i need to be logged into the system hosting graphql to be able to query externally, I cannot just use the endpoint url.
My developer has told me to do a POST request to mysystem/login and in the request body add {email: string, password: string}. I have tried a few different things and nothing is working. I have to log into the mysystem/login with email and password, then i will be able to connect to mygraphql endpoint from the code below.--i was assuming this portion would go where I have the //smt authentication notes. Any help on how to setup the post request and where it should be or how it should work would be much appreciated.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using SimpleJSON;
using UnityEngine;
using UnityEngine.Networking;
namespace graphQLClient
{
public class GraphQuery : MonoBehaviour
{
public static GraphQuery instance = null;
[Tooltip("The url of the node endpoint of the graphQL server being queried")]
public static string url;
public delegate void QueryComplete();
public static event QueryComplete onQueryComplete;
public enum Status { Neutral, Loading, Complete, Error };
public static Status queryStatus;
public static string queryReturn;
string authURL;
public static string LoginToken;
public class Query
{
public string query;
}
public void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
StartCoroutine("PostLogin");
}
private void Start()
{
LoginToken = "";
}
public static Dictionary<string, string> variable = new Dictionary<string, string>();
public static Dictionary<string, string[]> array = new Dictionary<string, string[]>();
// Use this for initialization
// SMT Authentication to ELP
// SMT Needed for Authentication--- if (token != null) request.SetRequestHeader("Authorization", "Bearer " + token);
//In the request body: {email: string, password: string}
//You’ll either get a 401 with an empty response or 201 with {token: string, user: object}
IEnumerator PostLogin()
{
Debug.Log("Logging in...");
string authURL = "https://myapp.com/login";
string loginBody = "{\"email\":\"myemail.com\",\"password\":\"mypassowrd\"}";
var request = new UnityWebRequest(authURL, "POST");
byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes(loginBody);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log("Login error!");
foreach (KeyValuePair<string, string> entry in request.GetResponseHeaders())
{
Debug.Log(entry.Value + "=" + entry.Key);
}
Debug.Log(request.error);
}
else
{
Debug.Log("Login complete!");
//Debug.Log(request.downloadHandler.text);
LoginToken = request.downloadHandler.text;
Debug.Log(LoginToken);
}
}
public static WWW POST(string details)
{
//var request = new UnityWebRequest(url, "POST");
details = QuerySorter(details);
Query query = new Query();
string jsonData = "";
WWWForm form = new WWWForm();
query = new Query { query = details };
jsonData = JsonUtility.ToJson(query);
byte[] postData = Encoding.ASCII.GetBytes(jsonData);
Dictionary<string, string> postHeader = form.headers;
//postHeader["Authorization"] = "Bearer " + downloadHandler.Token;
if (postHeader.ContainsKey("Content-Type"))
//postHeader["Content-Type"] = "application/json";
postHeader.Add("Authorization", "Bearer " + LoginToken);
else
//postHeader.Add("Content-Type", "application/json");
postHeader.Add("Authorization", "Bearer " + LoginToken);
WWW www = new WWW(url, postData, postHeader);
instance.StartCoroutine(WaitForRequest(www));
queryStatus = Status.Loading;
return www;
}
static IEnumerator WaitForRequest(WWW data)
{
yield return data; // Wait until the download is done
if (data.error != null)
{
Debug.Log("There was an error sending request: " + data.error);
queryStatus = Status.Error;
}
else
{
queryReturn = data.text;
queryStatus = Status.Complete;
}
onQueryComplete();
}
public static string QuerySorter(string query)
{
string finalString;
string[] splitString;
string[] separators = { "$", "^" };
splitString = query.Split(separators, StringSplitOptions.RemoveEmptyEntries);
finalString = splitString[0];
for (int i = 1; i < splitString.Length; i++)
{
if (i % 2 == 0)
{
finalString += splitString[i];
}
else
{
if (!splitString[i].Contains("[]"))
{
finalString += variable[splitString[i]];
}
else
{
finalString += ArraySorter(splitString[i]);
}
}
}
return finalString;
}
public static string ArraySorter(string theArray)
{
string[] anArray;
string solution;
anArray = array[theArray];
solution = "[";
foreach (string a in anArray)
{
}
for (int i = 0; i < anArray.Length; i++)
{
solution += anArray[i].Trim(new Char[] { '"' });
if (i < anArray.Length - 1)
solution += ",";
}
solution += "]";
Debug.Log("This is solution " + solution);
return solution;
}
}
}

Amazon MWS | RestSharp | The request signature we calculated does not match the signature you provided

I have written the following method to fetch all orders from Amazon. I'm using the RestSharp library to communicate with the API. Everytime I execute the request I get the following error:
<?xml version="1.0"?>
<ErrorResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
<Error>
<Type>Sender</Type>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.</Message>
</Error>
<RequestID>9f03f5b0-e4e4-4766-a554-00ff970b6b8c</RequestID>
</ErrorResponse>
That's very strange, because on the Amazon Scratchpad (https://mws.amazonservices.de/scratchpad/index.html) it is working and I also get the same signature (when I use the timestamp calculated on the Scratchpad) in my c# app.
C# Class
public async void FetchOrders()
{
RestClient client = new RestClient("https://mws.amazonservices.de");
client.DefaultParameters.Clear();
client.ClearHandlers();
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("AWSAccessKeyId", "xxxxxxxxxx");
parameters.Add("Action", "ListOrders");
parameters.Add("CreatedAfter", "2018-01-01T11:34:00Z");
parameters.Add("MarketplaceId.Id.1", "A1PA6795UKMFR9");
parameters.Add("SellerId", "xxxxxxxxx");
parameters.Add("SignatureVersion", "2");
parameters.Add("Timestamp", DateTime.UtcNow.ToString("s") + "Z");
parameters.Add("Version", "2013-09-01");
RestRequest request = new RestRequest("Orders/2013-09-01/", Method.POST);
string signature = AmzLibrary.SignParameters(parameters, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
request.AddParameter("Signature", signature);
foreach (KeyValuePair<string, string> keyValuePair in parameters)
{
request.AddParameter(keyValuePair.Key, keyValuePair.Value);
}
IRestResponse result = await client.ExecuteTaskAsync(request);
}
public static class AmzLibrary
{
public static string GetParametersAsString(IDictionary<String, String> parameters)
{
StringBuilder data = new StringBuilder();
foreach (String key in (IEnumerable<String>)parameters.Keys)
{
String value = parameters[key];
if (value != null)
{
data.Append(key);
data.Append('=');
data.Append(UrlEncode(value, false));
data.Append('&');
}
}
String result = data.ToString();
return result.Remove(result.Length - 1);
}
public static String SignParameters(IDictionary<String, String> parameters, String key)
{
String signatureVersion = parameters["SignatureVersion"];
KeyedHashAlgorithm algorithm = new HMACSHA1();
String stringToSign = null;
if ("2".Equals(signatureVersion))
{
String signatureMethod = "HmacSHA256";
algorithm = KeyedHashAlgorithm.Create(signatureMethod.ToUpper());
parameters.Add("SignatureMethod", signatureMethod);
stringToSign = CalculateStringToSignV2(parameters);
}
else
{
throw new Exception("Invalid Signature Version specified");
}
return Sign(stringToSign, key, algorithm);
}
private static String CalculateStringToSignV2(IDictionary<String, String> parameters)
{
StringBuilder data = new StringBuilder();
IDictionary<String, String> sorted =
new SortedDictionary<String, String>(parameters, StringComparer.Ordinal);
data.Append("POST");
data.Append("\n");
Uri endpoint = new Uri("https://mws.amazonservices.de/Orders/2013-09-01");
data.Append(endpoint.Host);
if (endpoint.Port != 443 && endpoint.Port != 80)
{
data.Append(":")
.Append(endpoint.Port);
}
data.Append("\n");
String uri = endpoint.AbsolutePath;
if (uri == null || uri.Length == 0)
{
uri = "/";
}
data.Append(UrlEncode(uri, true));
data.Append("\n");
foreach (KeyValuePair<String, String> pair in sorted)
{
if (pair.Value != null)
{
data.Append(UrlEncode(pair.Key, false));
data.Append("=");
data.Append(UrlEncode(pair.Value, false));
data.Append("&");
}
}
String result = data.ToString();
return result.Remove(result.Length - 1);
}
private static String UrlEncode(String data, bool path)
{
StringBuilder encoded = new StringBuilder();
String unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~" + (path ? "/" : "");
foreach (char symbol in System.Text.Encoding.UTF8.GetBytes(data))
{
if (unreservedChars.IndexOf(symbol) != -1)
{
encoded.Append(symbol);
}
else
{
encoded.Append("%" + String.Format("{0:X2}", (int)symbol));
}
}
return encoded.ToString();
}
private static String Sign(String data, String key, KeyedHashAlgorithm algorithm)
{
Encoding encoding = new UTF8Encoding();
algorithm.Key = encoding.GetBytes(key);
return Convert.ToBase64String(algorithm.ComputeHash(
encoding.GetBytes(data.ToCharArray())));
}
}
The problem was the "/" at the end of the Request. Remove it and everything works.
wrong
RestRequest request = new RestRequest("Orders/2013-09-01/", Method.POST);
right
RestRequest request = new RestRequest("Orders/2013-09-01", Method.POST);

HtmlAgilityPack obtain Title and meta

I try to practice "HtmlAgilityPack ", but I am having some issues regarding this. here's what I coded, but I can not get correctly the title and the description of a web page ...
If someone can enlighten me on my mistake :)
...
public static void Main(string[] args)
{
string link = null;
string str;
string answer;
int curloc; // holds current location in response
string url = "http://stackoverflow.com/";
try
{
do
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url);
HttpWReq.UserAgent = #"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5";
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
//url = null; // disallow further use of this URI
Stream istrm = HttpWResp.GetResponseStream();
// Wrap the input stream in a StreamReader.
StreamReader rdr = new StreamReader(istrm);
// Read in the entire page.
str = rdr.ReadToEnd();
curloc = 0;
//WebPage result;
do
{
// Find the next URI to link to.
link = FindLink(str, ref curloc); //return the good link
Console.WriteLine("Title found: " + curloc);
//title = Title(str, ref curloc);
if (link != null)
{
Console.WriteLine("Link found: " + link);
using (System.Net.WebClient client = new System.Net.WebClient())
{
HtmlDocument htmlDoc = new HtmlDocument();
var html = client.DownloadString(url);
htmlDoc.LoadHtml(link); //chargement de HTMLAgilityPack
var htmlElement = htmlDoc.DocumentNode.Element("html");
HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//meta[#name='description']");
if (node != null)
{
string desc = node.GetAttributeValue("content", "");
Console.Write("DESCRIPTION: " + desc);
}
else
{
Console.WriteLine("No description");
}
var titleElement =
htmlDoc.DocumentNode
.Element("html")
.Element("head")
.Element("title");
if (titleElement != null)
{
string title = titleElement.InnerText;
Console.WriteLine("Titre: {0}", title);
}
else
{
Console.WriteLine("no Title");
}
Console.Write("Done");
}
Console.Write("Link, More, Quit?");
answer = Console.ReadLine();
}
else
{
Console.WriteLine("No link found.");
break;
}
} while (link.Length > 0);
// Close the Response.
HttpWResp.Close();
} while (url != null);
}
catch{ ...}
Thanks in advance :)
Go about it this way:
HtmlNode mdnode = htmlDoc.DocumentNode.SelectSingleNode("//meta[#name='description']");
if (mdnode != null)
{
HtmlAttribute desc;
desc = mdnode.Attributes["content"];
string fulldescription = desc.Value;
Console.Write("DESCRIPTION: " + fulldescription);
}
I think your problem is here:
htmlDoc.LoadHtml(link); //chargement de HTMLAgilityPack
It should be:
htmlDoc.LoadHtml(html); //chargement de HTMLAgilityPack
LoadHtml expects a string with the HTML source, not the url.
And probably you want to change:
var html = client.DownloadString(url);
to
var html = client.DownloadString(link);
Have you used a breakpoint and gone line for line to see where the error might be occurring?
If you have, then Try something like this:
string result = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.Method = "GET";
try
{
using (var stream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(result);
Then carry over the rest of your code below the htmlDoc.LoadHtml
[HttpPost]
public ActionResult Create(WebSite website)
{
string desc = HtmlAgi(website.Url, "description");
string keyword = HtmlAgi(website.Url, "Keywords");
if (ModelState.IsValid)
{
var userId = ((CustomPrincipal)User).UserId;
r.Create(new WebSite
{
Description = desc,
Tags = keyword,
Url = website.Url,
UserId = userId,
Category = website.Category
});
return RedirectToAction("Index");
}
return View(website);
}
public string HtmlAgi(string url, string key)
{
//string.Format
var Webget = new HtmlWeb();
var doc = Webget.Load(url);
HtmlNode ourNode = doc.DocumentNode.SelectSingleNode(string.Format("//meta[#name='{0}']", key));
if (ourNode != null)
{
return ourNode.GetAttributeValue("content", "");
}
else
{
return "not fount";
}
}

Categories