jeudi 30 juin 2016

C# - HTTP POST request from with checkboxes

Trying to write a c# app to sign me up for all these newsletters, I have used HTTPFox to analyze the HTTP requests and construct the right POST parameters but WebRequest is throwing a 403 forbidden. I've done this successfully with simple forms but the huge # of controllers and the checkboxes are heavy.

HTTPFox Output:

POST data:

Parameter: Value
    _account_id 737
    _table_id   1
    _email_field    1.email
    _dedupe 1
    _static_update  1
    _rp http://ift.tt/29f7FbT
    _list_id    12
    _list_id    1
    _list_id    8
    _list_id    5
    _list_id    4
    _list_id    10
    _list_id    14
    _list_id    6
    _list_id    13
    _list_id    2035
    _list_id    7091
    _list_id    318
    _list_id    2
    _list_id    7294
    _list_id    317
    _list_id    8702
    _list_id    7298
    _list_id    7295
    _list_id    7297
    _list_id    10158
    _list_id    6419
    7.title Mr
    7.first_name    fname_redacted
    7.surname   sname_redacted
    1.email redactedg@outlook.com
    7.company   cname_redacted
    7.position  Analyst
    7.add1  23 liverpool road
    7.add2  
    7.add3  
    7.add4  liverpool
    7.add5  Bedfordshire
    7.postcode  l59uh
    7.telephone 1519838474
    7.sector    Accountants
    7_employees Less than 25
    7.turnover  
    7.registered_by 
    7.audit_question    

Headers:

Request_header: Value
(Request-Line)  POST /s/ HTTP/1.1
Host    newsco.msgfocus.com
User-Agent  Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-US,en;q=0.5
Accept-Encoding gzip, deflate, br
Referer http://ift.tt/297YUwP
Connection  keep-alive
Content-Type    application/x-www-form-urlencoded
Content-Length  752

C#:

    WebRequest request = WebRequest.Create("http://ift.tt/297YOoS");
    request.Method = "POST";
    string postData = "email=redacted%40outlook.com";
    postData += "&_account_id=737";
    postData += "&_table_id=1";
    postData += "&_email_field=1.email";
    postData += "&_dedupe=1";
    postData += "&_static_update=1";
    postData += "&_rp=http://ift.tt/29f7FbT";
    postData += "&_list_id=12";
    postData += "&_list_id=1";
    postData += "&_list_id=8";
    postData += "&_list_id=5";
    postData += "&_list_id=4";
    postData += "&_list_id=10";
    postData += "&_list_id=14";
    postData += "&_list_id=6";
    postData += "&_list_id=13";
    postData += "&_list_id=2035";
    postData += "&_list_id=7091";
    postData += "&_list_id=318";
    postData += "&_list_id=2";
    postData += "&_list_id=7294";
    postData += "&_list_id=317";
    postData += "&_list_id=8702";
    postData += "&_list_id=7298";
    postData += "&_list_id=7295";
    postData += "&_list_id=7297";
    postData += "&_list_id=10158";
    postData += "&_list_id=6419";
    postData += "&title=Mr";
    postData += "&first_name=fname_redacted";
    postData += "&surname=sname_redacted";
    postData += "&email=redactedg%40outlook.com";
    postData += "&company=cname_redacted";
    postData += "&position=Analyst";
    postData += "&add1=23 liverpool road";
    postData += "&add4=liverpool";
    postData += "&add5=bedfordshire";
    postData += "&postcode=l59uh";
    postData += "&telephone=1519838474";
    postData += "&sector=Accountants";
    postData += "&_employees=Less than 25";

    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = byteArray.Length;
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    WebResponse response = request.GetResponse(); // 403 forbidden

    Console.WriteLine(((HttpWebResponse)response).StatusDescription);
    dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();
    Console.WriteLine(responseFromServer);
    reader.Close();
    dataStream.Close();
    response.Close();
    Console.ReadLine();

WebResponse response = request.GetResponse(); // 403 forbidden




Aucun commentaire:

Enregistrer un commentaire