Sign up instagram Using C# - c#

so im working on an instagram tool. i need to Create an account with HttpWebRequest but somethings wrong with my code and it doesnt work here is the Code:
username = "something";
password = "something";
mail = " something#mail.com";
name = "something";
string postData = "email=" + mail + "&password=" + password + "&enc_password=%23PWD_INSTAGRAM_BROWSER%3A6%3A1583227313%3AAXFQANTXAGE5jwEKNbpJbvot0SGTp%2Bq7a0ckELnuYQnQLIJa2Th6UwIqdknx%2FDa8R7q1%2F2Bt4scBUrh%2B1aFDKL0H%2Fut5cyi3w1sIEPa1keAuNcNj9nAXo1oFzUYMhCGVB7qerse5hQfMjTVjNa4%3D&username=" + username + "&first_name=" + name + "&seamless_login_enabled=1&tos_version=row&opt_into_one_tap=false";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.instagram.com/accounts/emailsignup/");
byte[] postBytes = Encoding.ASCII.GetBytes(postData);
req.ContentLength = postBytes.Length;
req.CookieContainer = new CookieContainer();
req.Method = "POST";
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36";
req.Accept = "*/*";
req.ContentType = "application/x-www-form-urlencoded";
req.Referer = "https://www.instagram.com/accounts/emailsignup/";
req.Headers["Accept-Language"] = "de,en-US;q=0.7,en;q=0.3";
req.Headers["Accept-Encoding"] = "gzip, deflate";
req.Headers["X-Requested-With"] = "XMLHttpRequest";
Stream dataStream = req.GetRequestStream();
dataStream.Write(postBytes, 0, postBytes.Length);
dataStream.Flush();
dataStream.Close();
HttpWebResponse webResp = (HttpWebResponse)req.GetResponse(); //error in this line---> The remote server returned an error: (403) Forbidden.
Stream datastream = webResp.GetResponseStream();
StreamReader reader = new StreamReader(datastream);
string s = reader.ReadToEnd();
Console.WriteLine(s);

Related

HttpWebResponse returns wrong html

For get bitcoin price i did this :
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.blockchain.com/prices");
req.Method = "GET";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
req.ContentType = "text/html; charset=utf-8";
req.Referer = "";
req.KeepAlive = true;
req.Timeout = 25000;
req.AllowAutoRedirect = false;
CookieContainer cookieJar1 = new CookieContainer();
req.CookieContainer = cookieJar1;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
foreach (Cookie cookie in res.Cookies)
{
cookieJar1.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), "/", cookie.Domain));
}
Stream Stream = res.GetResponseStream();
StreamReader reader = new StreamReader(Stream);
string reader_str = reader.ReadToEnd();
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(reader_str);
HtmlAgilityPack.HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//p[#title='Bitcoin']");
string bitcoin_price_str = node.InnerText.Trim().Replace(",", "");
bitcoin_price = double.Parse(bitcoin_price_str);
reader.Close();
Stream.Close();
res.Close();
The reader_str is like this :
https://pastebin.com/jT66GzmA
Why reader_str has wrong html?
How can i fix this problem?

HttpWebRequest - cannot send a content-body with this verb-type

I have an application written in C# that use HttpWebRequest to send the request with body.
To add text to request I use this code:
WebRequest webRequest = WebRequest.Create(responseUri);
((HttpWebRequest)webRequest).Referer = responseUri;
((HttpWebRequest)webRequest).Host = "sts.mycompany.com";
((HttpWebRequest)webRequest).KeepAlive = true;
((HttpWebRequest)webRequest).AllowAutoRedirect = true;
((HttpWebRequest)webRequest).UseDefaultCredentials = true;
((HttpWebRequest)webRequest).UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36";
webRequest.ContentType = "application/x-www-form-urlencoded";
((HttpWebRequest)webRequest).Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
((HttpWebRequest)webRequest).Headers.Add("Cache-Control", "max-age=0");
((HttpWebRequest)webRequest).Headers.Add("Accept-Encoding", "gzip, deflate, br, peerdist");
((HttpWebRequest)webRequest).Headers.Add("Accept-Language", "en-US,en;q=0.5");
//((HttpWebRequest)webRequest).Headers.Add("Accept-Charset", "ISO-8859-2,utf-8;q=0.7,*;q=0.3");
((HttpWebRequest)webRequest).Headers.Add("Upgrade-Insecure-Requests", #"1");
((HttpWebRequest)webRequest).Headers.Add("DNT", #"1");
((HttpWebRequest)webRequest).CookieContainer = CookieContainer;
foreach (var cookie in CookieContainer.GetCookies(new Uri(responseUri)))
{
Console.WriteLine(cookie.ToString());
}
((HttpWebRequest)webRequest).Credentials = GetNetworkCredentials();
((HttpWebRequest)webRequest).AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
webRequest.Method = requestType.ToString();
string msg = String.Format("UserName={0}&Password={1}&AuthMethod=FormsAuthentication", System.Net.WebUtility.UrlEncode(username), System.Net.WebUtility.UrlEncode(password));
webRequest.ContentLength = msg.Length;
Stream reqStream = webRequest.GetRequestStream();
byte[] msgb = System.Text.Encoding.UTF8.GetBytes(msg);
reqStream.Write(msgb, 0, msgb.Length);
reqStream.Close();
var response = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string Result = sr.ReadToEnd();
var originalString = response.ResponseUri.OriginalString;
response.Close();
The error is in this line, when I want to add text to body:
byte[] msgb = System.Text.Encoding.UTF8.GetBytes(msg);
reqStream.Write(msgb, 0, msgb.Length);
error: Cannot send a content-body with this verb-type
and any additional informations.

How to get access to OAuth2 token for Yelp Fusion Api using C#

I am trying to get an access OAuth2 token for Yelp Fusion Api using C# as mentioned in the documentation: https://www.yelp.com/developers/documentation/v3/get_started
However, I am getting the error :
client_id or client_secret not found. Make sure to provide client_id and client_secret in the body with the application application/x-www-form-urlencoded
The following is the code snippet:
<code>
string baseURL = "https://api.yelp.com/oauth2/token";
Dictionary<string, string> query = new Dictionary<string, string>();
query["grant_type"] = "client_credentials";
query["client_id"] = CONSUMER_KEY;
query["client_secret"] = CONSUMER_SECRET;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseURL);
request.Accept = "application/json";
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
requestWriter.Write(query.ToString());
requestWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
Console.WriteLine(stream.ReadToEnd());
</code>
As per the Yelp Fusion documentation here you need to make a POST call and parameters should be sent in application/x-www-form-urlencoded format. So the method used above is incorrect.
This should help:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(baseURL);
webRequest.Method = "POST";
webRequest.AllowAutoRedirect = true;
webRequest.Timeout = 20 * 1000;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";
//write the data to post request
String postData = "client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&grant_type=client_credentials";
byte[] buffer = Encoding.Default.GetBytes(postData);
if (buffer != null)
{
webRequest.ContentLength = buffer.Length;
webRequest.GetRequestStream().Write(buffer, 0, buffer.Length);
}
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string strResponse = reader.ReadToEnd();
Please note that the example above will return data in String format. To read the actual values, you will have to serialize the data.
Edit: Since March 1, 2018, the authentication process for yelp fusion API has changed. This is not applicable any more.

The remote server returned an error: (403) Forbidden when post xml data

I'm getting the error
The remote server returned an error: (403) Forbidden.
when post some xml data to a api. Below is the code.
I have read some other questions from stack overflow but doesn't fix.
public static XmlDocument PlaceOrder(string Url, string Method, string payload)
{
Encoding enc = System.Text.Encoding.GetEncoding(1252);
HttpWebRequest apiCall = (HttpWebRequest)WebRequest.Create(Url);
byte[] byteArray = ASCIIEncoding.UTF8.GetBytes(payload);
apiCall.Accept = "*/*";
apiCall.KeepAlive = false;
apiCall.AllowAutoRedirect = false;
apiCall.Method = "POST";
apiCall.ContentType = "text/xml; encoding='utf-8'";
apiCall.ContentLength = byteArray.Length;
apiCall.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36";
apiCall.Headers.Add("Accept-Encoding", "gzip, deflate, br");
apiCall.Headers.Add("Accept-Language", "en-US,en;q=0.8");
apiCall.CookieContainer = new CookieContainer();
apiCall.UseDefaultCredentials = true;
apiCall.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream dataStream = apiCall.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
HttpWebResponse webresponse;
webresponse = (HttpWebResponse)apiCall.GetResponse();
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
string response = loResponseStream.ReadToEnd();
XmlDocument toReturn = new XmlDocument();
toReturn.LoadXml(response);
return toReturn;
}

HttpWebRequest SSL Authorization

How login to the resource with SSL? I sent data with help HttpWebRequest on url. In half of cases return redirect to login page and dont't get cookies.
Maybe, make request on 443 port? If yes, how this make with help HttpWebRequest?
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
const string root = #"https://****/";
var loginUrl = string.Format(#"{0}csologin/login.jsf", root);
var login = Settings.Default.PacerLogin;
var pass = Settings.Default.PacerPassword;
var postData =
string.Format(
#"login=login&login:loginName={0}&login:password={1}&login:clientCode=&login:j_idt139=&javax.faces.ViewState=stateless",
login, pass);
var request = (HttpWebRequest)WebRequest.Create(loginUrl);
request.CookieContainer = this.cookieContainer;
var ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData);
request.Proxy = null;
request.Credentials = new NetworkCredential(login, pass);
request = SetHeaders(request, loginUrl);
request.ContentLength = postBytes.Length;
request.Method = WebRequestMethods.Http.Post;
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
And Headers
request.UserAgent =
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36";
request.Headers.Add("Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6,ru;q=0.4");
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.KeepAlive = true;
request.Headers["Cache-Control"] = "max-age=0";
request.AllowAutoRedirect = allowRedirect;
request.Connection =
request.Referer = referer;
request.ContentType = "application/x-www-form-urlencoded";
request.ServicePoint.Expect100Continue = false;

Categories

Resources