Logging into a website by code - c#

I have to download an excel file from a website through code. But the downloading requires logging into a website. So I am confused that what data should I post to login from code.
Here's the request from chrome:
Request URL:https://206.82.192.135:4100/wgcgi.cgi?action=fw_logon&style=fw_logon.xsl&fw_logon_type=status
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:884
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryPAzPZF4h5AQHA6Ic
Cookie:domain=null; username=CandA
Host:206.82.192.135:4100
Origin:https://206.82.192.135:4100
Referer:https://206.82.192.135:4100/wgcgi.cgi?action=fw_logon&style=fw_logon.xsl&fw_logon_type=status
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Query String Parametersview sourceview URL encoded
action:fw_logon
style:fw_logon.xsl
fw_logon_type:status
Request Payload
------WebKitFormBoundaryPAzPZF4h5AQHA6Ic
Content-Disposition: form-data; name="fw_username"
[username appears here]
------WebKitFormBoundaryPAzPZF4h5AQHA6Ic
Content-Disposition: form-data; name="fw_password"
[password appears here]
------WebKitFormBoundaryPAzPZF4h5AQHA6Ic
Content-Disposition: form-data; name="fw_domain"
Firebox-DB
------WebKitFormBoundaryPAzPZF4h5AQHA6Ic
Content-Disposition: form-data; name="fw_domain"
Firebox-DB
------WebKitFormBoundaryPAzPZF4h5AQHA6Ic
Content-Disposition: form-data; name="submit"
Login
------WebKitFormBoundaryPAzPZF4h5AQHA6Ic
Content-Disposition: form-data; name="action"
fw_logon
------WebKitFormBoundaryPAzPZF4h5AQHA6Ic
Content-Disposition: form-data; name="style"
fw_logon_progress.xsl
------WebKitFormBoundaryPAzPZF4h5AQHA6Ic
Content-Disposition: form-data; name="fw_logon_type"
logon
------WebKitFormBoundaryPAzPZF4h5AQHA6Ic--
Response Headersview source
Connection:close
Content-Length:289
Content-Type:text/xml
Date:Wed, 21 Aug 2013 13:00:10 GMT
Server:None

If you search for a simple solution try to use the WebBrowser control. You can iterate through all of the items of the page and set username and password, then genaerate a click on the login button.

I explored more and found that I was not being authenticated by the SSL and I was getting this exception:
Could not establish trust relationship for the SSL/TLS secure channel
So I got a piece of code from
this
System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };
and it solved my problem

Related

Requesting to github-api using .net HttpClient says Forbidden

I am trying to getting data from this github api link https://api.github.com/users/arif2009 . It works fine using posman get request.
But if i try to get data using .net HttpClient then it says Forbidden.
C# code:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.github.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.GetAsync("users/arif2009");
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsAsync<GithubUser>();
}
}
Response:
Can anybody tell me where i made the mistake?
I took a look at your requests using Telerik's Fiddler and found the following.
Request with your code:
GET https://api.github.com/users/arif2009 HTTP/1.1
Accept: application/json
Host: api.github.com
Request from Postman:
GET https://api.github.com/users/arif2009 HTTP/1.1
Host: api.github.com
Connection: keep-alive
Accept: application/json
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36
Postman-Token: d560ee28-b1e8-ece5-2612-87371ddcb295
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,ja-JP;q=0.8,ja;q=0.7,en-US;q=0.6
The obvious missing header seemed to be "User-Agent", so I added this:
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("product", "1")); // set your own values here
Which produced the following request:
GET https://api.github.com/users/arif2009 HTTP/1.1
Accept: application/json
User-Agent: product/1
Host: api.github.com
And returned the following response:
HTTP/1.1 200 OK
Date: Wed, 17 Jul 2019 15:19:35 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 1249
Server: GitHub.com
Status: 200 OK
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 52
X-RateLimit-Reset: 1563380375
Cache-Control: public, max-age=60, s-maxage=60
Vary: Accept
ETag: "1df3e0be6e824ca684f27963806533da"
Last-Modified: Tue, 16 Jul 2019 05:58:59 GMT
X-GitHub-Media-Type: github.v3
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy: default-src 'none'
Vary: Accept-Encoding
X-GitHub-Request-Id: D5E0:8D63:8BE92C:A799AE:5D2F3C86
{"login":"arif2009","id":6396346,"node_id":"MDQ6VXNlcjYzOTYzNDY=","avatar_url":"https://avatars0.githubusercontent.com/u/6396346?v=4","gravatar_id":"","url":"https://api.github.com/users/arif2009","html_url":"https://github.com/arif2009","followers_url":"https://api.github.com/users/arif2009/followers","following_url":"https://api.github.com/users/arif2009/following{/other_user}","gists_url":"https://api.github.com/users/arif2009/gists{/gist_id}","starred_url":"https://api.github.com/users/arif2009/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arif2009/subscriptions","organizations_url":"https://api.github.com/users/arif2009/orgs","repos_url":"https://api.github.com/users/arif2009/repos","events_url":"https://api.github.com/users/arif2009/events{/privacy}","received_events_url":"https://api.github.com/users/arif2009/received_events","type":"User","site_admin":false,"name":"Arif","company":"#BrainStation-23 ","blog":"https://arif2009.github.io/","location":"Bangladesh","email":null,"hireable":true,"bio":"Software Engineer | Full Stack | Web Developer | Technical Writer","public_repos":15,"public_gists":2,"followers":9,"following":7,"created_at":"2014-01-14T05:03:47Z","updated_at":"2019-07-16T05:58:59Z"}
this API reference from github says that 'User-Agent' is a required header:
All API requests MUST include a valid User-Agent header. Requests with no User-Agent header will be rejected.
Postman automatically adds its own User-Agent to the call when this is not provided by the user. (This is nicely demonstrated by #John's answer).
simply adding this header will resolve your issue:
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("yourAppName", "yourVersionNumber"));
You may try to put the complete URI into the GetAsync instead using the BaseAddress, as you do not give the GetAsync an URI, but only a string.
When sending a HttpRequestMessage with a relative Uri, the message Uri will be added to the BaseAddress property to create an absolute Uri.
https://learn.microsoft.com/de-de/dotnet/api/system.net.http.httpclient.baseaddress?view=netframework-4.8

I want to upload video on amazon cloud using REST api

I have shared Documentation to upload file from this link.
Upload File
Uploads the file content along with its metadata information.
POST : {{contentUrl}}/nodes?suppress={suppress}
suppress : (Optional)
deduplication: disables checking for duplicates when uploading Body Parameters:
Multi-form part
--------- metadata ------------
name (required) : file name. Max to 256 Characters. kind (required) : "FILE" labels (optional) : Extra information which is indexed. For example the value can be "PHOTO" properties (optional) : List of properties to be added for the file. parents(optional) : List of parent Ids. If no parent folders are provided, the file will be placed in the default root folder.
---------content ------------
File Bytes
Sample Request:
POST /cdproxy/nodes?localId=testPhoto
HTTP/1.1
Host: content-na.drive.amazonaws.com
Authorization: Bearer
Atza|IQEBLjAsAhReYeezFcFdYzqrFOJGv3EG
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="metadata"
{"name":"fooo.jpg","kind":"FILE"}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="content";
filename="db5df4870e4e4b6cbf42727fd434701a.jpg"
Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
**cURL Request:**
curl -v -X POST --form
'metadata={"name":"testVideo1","kind":"FILE"}' --form
'content=#sample_iTunes.mp4'
'https://content-na.drive.amazonaws.com/cdproxy/nodes?localId=testVideo1&suppress=deduplication'
--header "Authorization: Bearer
Atza|IQEBLjAsAhQ5zx7pKp9PCgCy6T1JkQjHHOEzpwIUQM"
Fiddler:
I have written code and sent request.
This is the request i have sent using Fiddler
POST
https://content-na.drive.amazonaws.com/cdproxy//nodes?suppress=deduplication
HTTP/1.1 Authorization: Bearer
Atza|IQEBLjAsAhQXfR4gxrstIKB8jjCbbsTIRhad1QIUZgLJFXgenPSsYLp6VHeL2rEBQ6RFSQYAR847Pvr8bnF4wc-qAlP2oKbAxogI_KJBopMzbTwt3n1DgJ9D8VJTikjdkXH7_595n4ElGhAhKJybhiXBlZLDh7ScXiFC6g4MFfkVRMKoKWAf4gRhhYGTLX2nBSHWIYx5kpzbS0QNgOUS_Hluodfq0j5gf8FeNB3YG6q-KiQMdPbcXJFlKs28f_cSWnQvYyLu0c8YzcJZpjL3CTrabnGpF5KOM6ie71Q2mS3ncL71tEO5wu3MVEJZZtg00MKM5nkGAzH1hCHqA0PWmsLsQVm0X684Seje7aSMYDlh0hcRpgc4Y7fusEndnprz5EO9FftNDN3lbvxszgAR0DhoOg
Content-Type: multipart/form-data Host: content-na.drive.amazonaws.com
Content-Length: 2100825 Expect: 100-continue
------WebKitFormBoundary8d2ff0fafd6a750 Content-Disposition: form-data; name="metadata"
{"name":"C:\Users\jafar.baltidynamolog\Downloads\SampleVideo_360x240_2mb.mp4","kind":"FILE"}
------WebKitFormBoundary8d2ff0fafd6a750 Content-Disposition: form-data; name="content";
filename="C:\Users\jafar.baltidynamolog\Downloads\SampleVideo_360x240_2mb.mp4" Content-Type: video/mp4
��� ftypisom���isomiso2avc1mp41���free��LmdatT Lj����L5
��P�L"�:�ԮoUNx��V���qFM4��w�ח�l�iݧ����o��͑s����z���/e��ۢ�a���G��y�j�W-&{x�w��>�I���6�v�u�j;��pk#�=�K�ZO.a�yy
-w(���ژ�#S�Rpt<{�ڿZ38xt���x�[��ɒ�&�lsT��4P��rMXv��Ѹ%A�5���C�hH& ���P� Z���翷�n�����z���5t��D��M%�}���i���P|�k��#�>�#�tq�O�K���sy��=�\��9���)���Ɏ��������T��(��=v����
)D���R;zKBx��PU� ���E�/bB��)p�h,6��`��� ���XH2
Problem is that i am getting Error 500 internal Error. Kindly help me understand if some thing is missing or wrong. I am using this code to upload video. Kindly help me

Photo Upload On Facebook

I want to Upload Photo to Facebook Page using HttpWebRequest
and I don't want to use Facebook Graph API ..
via live http header i get the url and data which is need to be posted
user=XXXXXXXXXXXXX&_a=1&_dyn=XXXXXXXXXXXX&_req=s&fb_dtsg=AQAszPoc&ttstamp=26581651151228011199&__rev=1141676">https://upload.facebook.com/ajax/composerx/attachment/media/saveunpublished?target_id=XXXXXXXXXXXXXX1&_user=XXXXXXXXXXXXX&_a=1&_dyn=XXXXXXXXXXXX&_req=s&fb_dtsg=AQAszPoc&ttstamp=26581651151228011199&__rev=1141676
DATA which will be used for sending with url
-----------------------------3300296215819
Content-Disposition: form-data; name="fb_dtsg"
AQAszPoc
-----------------------------3300296215819
Content-Disposition: form-data; name="source"
8
-----------------------------3300296215819
Content-Disposition: form-data; name="profile_id"
xxxxxxxxxxx
-----------------------------3300296215819
Content-Disposition: form-data; name="grid_id"
u_6_0
-----------------------------3300296215819
Content-Disposition: form-data; name="qn"
a9f0f5a0f2104b0ef0656f692cda9893
-----------------------------3300296215819
Content-Disposition: form-data; name="0"; filename="1794679_698919370150942_1311355907_n.jpg"
Content-Type: image/jpeg
YOUR DATA OR IMAGE
-----------------------------85332783618735
Content-Disposition: form-data; name="upload_id"
1025
-----------------------------85332783618735--
I m not sure how to get this value
Content-Disposition: form-data; name="qn"
a9f0f5a0f2104b0ef0656f692cda9893
can someone helpe me out
everytime when i tried to upload i got error
for (;;);{"__ar":1,"error":1357032,"errorSummary":"Sorry, we got confused","errorDescription":"Please try refreshing the page or closing and re-opening your browser window.","payload":null,"bootloadable":{},"ixData":[]}
reason why i don't want to use FaceBook Graph API is that whenever we post/upload it display message " 33 minutes ago via Graph API Explore"
please help me out
Thanks

Model Binder not working on first request after App Pool recycle

I have a problem with the model binder. Each time after I recycle the App Pool, the first request to the server fails to perform model binding. Request.InputStream has the proper request parameters, but the Action parameters are null and Request.Params does not contain them either.
This appears to happen on the first POST request with JSON. The real first request is a GET to Index(), which returns the view. After that, the client makes an AJAX request that fails.
This is what my Action looks like:
public async Task<ActionResult> GetQueryCounters(string query) { ... }
This is the contents of Request.InputStream on the first request (it's the same on subsequent requests):
{"query":"SELECT count(*) FROM accounts"}
This is the raw request as seen in Fiddler:
POST https://[url]/GetQueryCounters HTTP/1.1
Host: [url]
Connection: keep-alive
Content-Length: 281
Origin: https://[url]
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36
Content-Type: application/json; charset=UTF-8
Accept: application/json, text/javascript, */*; q=0.01
x-ajaxRequest: true
X-Requested-With: XMLHttpRequest
__RequestVerificationToken: [snip]
Referer: https://[url]
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,he;q=0.6
Cookie: [snip]
{"query":"SELECT count(*) FROM accounts"}
What could be causing this?

multipart/form-data + Sockets = Server not Responding

I'm using my own HTTP wrapper using sockets. So far everything works except multipart/form-data POST. When I send the data to server, it simply doesn't respond, so I'm guessing I'm missing something in my POST like I would need "\r\n\r\n" for regular requests.
This is my request:
POST /getproxies/get.php HTTP/1.1
Host: myhost.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Cookie: sid=UtozR43zQQAtVA2Np0KP_ZLnO0IYwwU1S_Tt4Z3N80wctL;
Connection: Close
Content-Type: multipart/form-data; boundary=---------------------------4664151417711
Content-Length: 411
---------------------------4664151417711
Content-Disposition: form-data; name="proxy_count"
20 proxies
---------------------------4664151417711
Content-Disposition: form-data; name="u"
13
---------------------------4664151417711
Content-Disposition: form-data; name="opt_1"
---------------------------4664151417711
Content-Disposition: form-data; name="opt_2"
GET
---------------------------4664151417711--
When I send the data, the connection is open, like it would be waiting on the server, but server doesn't respond. Any ideas what am I missing here? Thanks
Your boundary isn't matching the one declared on the boundary= part.
Content-Type: multipart/form-data; boundary=AaB03x
--AaB03x
Content-Disposition: form-data; name="submit-name"
Larry
--AaB03x
Content-Disposition: form-data; name="files"; filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--AaB03x--
RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types:
The Content-Type field for multipart entities requires one parameter,
"boundary". The boundary delimiter line is then defined as a line
consisting entirely of two hyphen characters ("-", decimal value 45)
followed by the boundary parameter value from the Content-Type header
field, optional linear whitespace, and a terminating CRLF.
So I guess this should work:
Content-Type: multipart/form-data; boundary=54472172614771
Content-Length: 47
--54472172614771
Your data
--54472172614771--
Please note the boundary specified in the boundary parameter of the Content-Type header should match the boundary you use in the payload. Also note the boundaries also count towards the Content-Length.

Categories