Simulating a full post request - c#

I am trying to simulate a post request using WebClient; However, When logging in using Firefox and debugging the request with firebug i find that after the POST request it automatically do some GET requests while using my code only do the POST request
MY CODE
//Handler is an overridden WebClient Class
private async Task<byte[]> Post(string uri, string[] data)
{
var postData = new NameValueCollection();
foreach (var info in data.Select(var => var.Split('=')))
{
postData.Add(info[0], info[1]);
}
return await Handler.UploadValuesTaskAsync(new Uri(uri), postData);
}

I know this isn't what you are asking for, AND its in VB, but hopefully it can help to point you in the right direction. It is what I use to make post requests on one of our websites. It works for simulating the POST data, hopefully you can incorporate some of that into what you are doing.
Dim postData As String = String.Format("RedirectLocation=RequestMethod=&username={0}&password={1}", _username, _password)
Dim _loginRequest As HttpWebRequest = WebRequest.Create(loginurl)
With _loginRequest
.Method = "POST"
.ContentLength = postData.Length
.ContentType = "application/x-www-form-urlencoded"
.KeepAlive = True
.AllowAutoRedirect = False
.CookieContainer = New CookieContainer
Using writer As New StreamWriter(.GetRequestStream)
writer.Write(postData)
End Using
.Timeout = tsTimeOut.TotalMilliseconds
_loginResponse = .GetResponse()
End With

Related

Xamarin.Android: How Can I Pass WebView Login Creds Into HttpWebRequest?

I am trying to pass login creds from a WebView into an HttpWebRequest but not having any luck getting an authenticated response. I am able to successfully make the request, but the response is acting like I haven't logged in. My app has 5 WebViews contained within Fragment s and I'm logged in on all of them. I've tried using the CookieSyncManager but it's deprecated and .Sync() didn't work. I've tried a lot of different ways of passing the cookies into the HttpRequest with no success and many hours spent.
One would think this is a simple request; user has logged in within the app; they should be authenticated for all requests. Here's the closest that I've gotten, but the response string is still not the same as through my authenticated WebView :
This attempt parses each Cookie into a string and adds it
public string _cookieString { get; set; }
private class ExtWebViewClient : WebViewClient
{
TheFragment5 _fm5 = new TheFragment5();
public override void OnPageFinished(WebView view, string url)
{
var cookieHeader = Android.Webkit.CookieManager.Instance.GetCookie(url);
var cookiePairs = cookieHeader.Split('&');
_fm5._cookieString = "";
foreach (var cookiePair in cookiePairs)
{
var cookiePieces = cookiePair.Split('=');
if (cookiePieces[0].Contains(":"))
cookiePieces[0] = cookiePieces[0].Substring(0, cookiePieces[0].IndexOf(":"));
cookies.Add(new Cookie
{
Name = cookiePieces[0],
Value = cookiePieces[1]
});
}
foreach (Cookie c in cookies)
{
if (_fm5._cookieString == "")
{
_fm5._cookieString = c.ToString();
}
else
{
_fm5._cookieString += c.ToString();
}
}
}
}
I've also tried just doing:
_fm5._cookieString = cookieHeader.ToString();
but neither of those attempts is working when I add the cookie string into my HttpRequest :
public async void GetNotificationText(string url)
{
//var _cmhc = _cookieMan.HasCookies;
await Task.Run(() =>
{
_notificationHttpRequestInProgress = true;
try
{
var _ctxxx = Android.App.Application.Context;
//URL _url2 = new URL("https://bitchute.com/notifications/");
//HttpURLConnection conn = (HttpURLConnection)_url2.OpenConnection();
//conn.ReadTimeout = 10000 /* milliseconds */;
//conn.ConnectTimeout = 15000 /* milliseconds */;
////conn.SetRequestProperty("Cookie", cookies);
//conn.Connect();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
Uri uri = new Uri(url);
var _req = request;
var _uriii = uri;
var _cookiesss = _fm5._cookieString;
_cookieCon.SetCookies(uri, _cookiesss);
request.CookieContainer = _cookieCon;
//request.CookieContainer.SetCookies(uri, _cookiesss);
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
_notificationRawText = reader.ReadToEnd();
Console.WriteLine(_notificationRawText);
_rawNoteText = _notificationRawText;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
_notificationHttpRequestInProgress = false;
});
}
This returns, but not the authenticated webtext request; I get the same response any user would get on a browser having never logged in. If I were to browse out to this same url on any WebView in my app, I'd get a completely different response.
You will also notice some commented out code that was another failed attempt at adding the cookies into a connection. I had also tried using HttpURLConnection.SetRequestProperty("Cookie", cookies);
where cookies was a CookieCollection and that didn't work either. The code is mostly commented out and layered because I've been trying this for days.
Does anyone know how I can pass WebView cookies into an HttpRequest using Xamarin.Android?
I am putting this code below in Fragment5 of my app; you can see and compile the full context here:
https://github.com/hexag0d/BitChute_Mobile_Android_BottomNav/blob/NotificationAdder/Fragments/TheFragment5.cs
I'm not sure exactly why the above example didn't work; maybe if you're better at .NET than I am, you could figure it out. However, I was able to successfully pass WebView creds into an HttpClient by following these steps, which are returning an authenticated response. This may not be the most elegant way of doing it, but you can always refine my answer, or post a better one.
What I had to do was set the HttpClient.DefaultRequestHeaders using the .Add() method like this: _client.DefaultRequestHeaders.Add("Cookie", TheFragment5._cookieHeader);
I got the CookieHeader (which is just a string btw) like this:
//instantiate a string that will house our cookie header
public static string _cookieHeader;
//you might want to make it private to prevent abuse
//but this example is just for demonstration
//the thing is we need a string to house our headers in scope of both the WebView and the HttpClient
//extend the WebViewClient
private class ExtWebViewClient : WebViewClient
{
public override void OnPageFinished(WebView view, string url)
{
//I get the cookies when the page finishes loading because
//then we know the cookie has our login cred header
//also, most of the previous examples got the cookies OnPageFinished
TheFragment5._cookieHeader = Android.Webkit.CookieManager.Instance.GetCookie(url);
}
}
Then we need another method for the HttpClient and HttpClientHandler ... mine scans a webpage for notification text.
public async void GetNotificationText(string url)
{
await Task.Run(() =>
{
/* this line is pretty important,
we need to instantiate an HttpClientHandler
then set it's UseCookies property to false
so that it doesn't override our cookies
*/
HttpClientHandler handler = new HttpClientHandler() { UseCookies = false };
try
{
Uri _notificationURI = new Uri("https://bitchute.com/notifications/");
//instantiate HttpClient using the handler
using (HttpClient _client = new HttpClient(handler))
{
//this line is where the magic happens;
//we set the DefaultRequestHeaders with the cookieheader we got from WebViewClient.OnPageFinished
_client.DefaultRequestHeaders.Add("Cookie", TheFragment5._cookieHeader);
//do a GetAsync request with our cookied up client
var getRequest = _client.GetAsync("https://bitchute.com/notifications/").Result;
//resultContent is the authenticated html string response from the server, ready to be parsed =]
var resultContent = getRequest.Content.ReadAsStringAsync().Result;
/*
I was writing to console to check the
response.. for me, I am now getting
the authenticated notification html
page
*/
Console.WriteLine(resultContent);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Hope this helps you, posting for future reference, especially for people using Xamarin.Android.

Can't do POST request on C# to blockr.io

Trying to write a method to push a TX, I never programatically done a POST request, so I'm clearly messing somewhere bad.
According to the documentation from blockr, I'm supposed to do this:
To publish a transaction make a POST (!) request with your transaction
hex to the push API.
Using curl this would be like (shell example):
curl -d '{"hex":"TX_HASH"}' http://btc.blockr.io/api/v1/tx/push
I'm getting 500 errors left and right.
I'm doing this on C#, could someone help?
Post("http://btc.blockr.io/api/v1/tx/push", "hex", HexString);
public static void Post(string RequestURL, string Post1, string Post2)
{
using (var wb = new WebClient())
{
var data = new NameValueCollection();
data[Post1] = Post2;
var response = wb.UploadValues(RequestURL, "POST", data);
}
}
By default UploadValues doesn't format the data in json, you could format it yourself:
public static void Post(string RequestURL, string Post1, string Post2)
{
using (var wb = new WebClient())
{
var data = string.Format("{0}\"{1}\":\"{2}\"{3}", "{", Post1, Post2, "}");
var response = wb.UploadString(RequestURL, "POST", data);
}
}
Or use a JSON serializer such as NewtonSoft

Get URL using HttpClient C# .NET

I am trying to get the URL of a page using HttpClient. I've previously only used HttpWebRequest, but I need to make this an async method.
In the code below, myUri always returns null which results in throwing an exception when I try to handle it later on.
Is the location header the wrong thing to be using?
string myUrl = "http://www.example.com/";
Uri myUri= new Uri(myUrl);
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(myUri))
{
if (response.IsSuccessStatusCode)
{
myUri= response.Headers.Location;
Debug.WriteLine("True "+ myUri);
}
else {
Debug.WriteLine("False " + myUri);
}
}
It's because HttpClient will automatically follows redirects. If you need the URL a page redirects to, you need to stop it from automatically following:
Change your code to the following:
string myUrl = "http://www.example.com/";
Uri myUri= new Uri(myUrl);
HttpClientHandler httpClientHandler = new HttpClientHandler();
httpClientHandler.AllowAutoRedirect = false;
using (HttpClient client = new HttpClient(httpClientHandler))
Here is an async way to resolve the final redirect URL:
public static async Task<string> ResolveFinalRedirectAsync(string url)
{
try
{
var req = WebRequest.CreateHttp(url);
req.AllowAutoRedirect = true;
var res = await req.GetResponseAsync();
return res.ResponseUri.AbsoluteUri;
}
catch
{
Console.WriteLine("Couldn't resolve '{0}'", url);
}
return null;
}
See #Rob's answer about AllowAutoRedirect.
Once you do that, note
The line
if (response.IsSuccessStatusCode)
evaluates to false if you receive a HTTP 301 redirect (anything outside of the 200-299 range)
A value that indicates if the HTTP response was successful. true if HttpStatusCode was in the Successful range (200-299); otherwise false.
(source)
OK thanks I'm trying to get redirected URLs anyway
If you prevent automatically following redirects, you will get an HTTP response in the 3xx range for the redirect. Your check for valid codes will have to be modified accordingly.

How to handle C# .NET POST and GET commands

The current project I am working on requires a 2 way communication from the bot to my website.
Supposing the example URL is www.example.com/foobar.php or something, can you explain me how to POST and GET data from there?
Thanks a lot.
P.S. - Using webclient right?
I'd suggest using RestSharp. It's a lot easier than using WebClient, and gives you a lot more options:
var client = new RestClient("http://www.example.com/");
//to POST data:
var postRequest = new RestRequest("foo.php", Method.POST);
postRequest.AddParameter("name", "value");
var postResponse = client.Execute(postRequest);
//postResponse.Content will contain the raw response from the server
//To GET data
var getRequest = new RestRequest("foo.php", Method.GET);
getRequest.AddParameter("name", "value");
var getResponse = client.Execute(getRequest);
Yes, you can use WebClient:
using (WebClient client = new WebClient())
{
NameValueCollection nvc = new NameValueCollection()
{
{ "foo", "bar"}
};
byte[] responseBytes = client.UploadValues("http://www.example.com/foobar.php", nvc);
string response = System.Text.Encoding.ASCII.GetString(responseBytes);
}
You can use WebClient
Look up method UploadString and DownloadString

C# Expect100Continue header request

I am facing the problem with posting username and password with different domains - one submits the form successfully while the other doesn't(the form data is empty)! The html code on both domains is the same. Here is the sample code- the commented domain doesn't post: Any Help is greatly appreciated!
Note: the domain that runs on nginx posts data successfully while the other on apache doesn't if at all it has got something to do with servers
public class CookieAwareWebClient : System.Net.WebClient
{
private System.Net.CookieContainer Cookies = new System.Net.CookieContainer();
protected override System.Net.WebRequest GetWebRequest(Uri address)
{
System.Net.WebRequest request = base.GetWebRequest(address);
if (request is System.Net.HttpWebRequest)
{
var hwr = request as System.Net.HttpWebRequest;
hwr.CookieContainer = Cookies;
}
return request;
}
}
# Main function
NameValueCollection postData = new NameValueCollection();
postData.Add("username", "abcd");
postData.Add("password", "efgh");
var wc = new CookieAwareWebClient();
//string url = "https://abcd.example.com/service/login/";
string url = "https://efgh.example.com/service/login/";
wc.DownloadString(url);
//writer.WriteLine(wc.ResponseHeaders);
Console.WriteLine(wc.ResponseHeaders);
byte[] results = wc.UploadValues(url, postData);
string text = System.Text.Encoding.ASCII.GetString(results);
Console.WriteLine(text);
The problem was with Expect100Continue header being added automatically each time when a request was made through the program which wasn't handled well on Apache. You have to set the Expect100Continue to false each time when a request is made in the following way. Thanks for the Fiddler lead although I could see it through the dumpcap tool on Amazon EC2 instance! Here is the solution!
# Main function
NameValueCollection postData = new NameValueCollection();
postData.Add("username", "abcd");
postData.Add("password", "efgh");
var wc = new CookieAwareWebClient();
var uri = new Uri("https://abcd.example.com/service/login/");
var servicePoint = ServicePointManager.FindServicePoint(uri);
servicePoint.Expect100Continue = false;
wc.DownloadString(uri);
Console.WriteLine(wc.ResponseHeaders);
byte[] results = wc.UploadValues(uri, postData);
string text = System.Text.Encoding.ASCII.GetString(results);
Console.WriteLine(text);

Categories

Resources