Rewrite with regular expressions - c#

Disposition: form-data; name="To"
ang <ang11#google.com>
-----------------------------7da3d81f160588
Content-Disposition: form-data; name="CC"
-----------------------------7da3d81f160588
Content-Disposition: form-data; name="BCC"
-----------------------------7da3d81f160588
Content-Disposition: form-data; name="Subject"
MySubject
-----------------------------7da3d81f160588
Content-Disposition: form-data; name="File"; filename=""<br/>
Content-Type: application/octet-stream
-----------------------------7da3d81f160588
Content-Disposition: form-data; name="cf_loader"
on
-----------------------------7da3d81f160588
Content-Disposition: form-data; name="File"; filename=""
Content-Type: application/octet-stream
-----------------------------7da3d81f160588
Content-Disposition: form-data; name="template_id"
-----------------------------7da3d81f160588
Content-Disposition: form-data; name="Body"
MyBody
I have written code that get information and writes in an KeyValuePair. As follow:
Key - Value
To - ang<\ang11#google.com>
CC - Empty
BCC - Empty
Subject - MySubject and etc.
My code is used of class String for this. I need to use regular expressions, but I do not understand them. Help please. Thanks.

A MIME parser should not be written with regular expressions. The problem is that you require a parser with state and that doesn't really fit regular expressions.
You can however parse parts with regular expressions, so for example:
First split the MIME body up into parts by splitting on the boundary: var parts = mime.Split("-----------------------------7da3d81f160588");;
Then, extract all the headers using a part.Split(new { "\r\n" }, 2, StringSplitOptions.None);;
Then, parse the headers. This may be done using a regular expression, but it's probably easier to do it like this: headerLine.Split(new [] { ':' }, 2).Select(p => p.Trim());.
Now, these separate parts can also be using regular expressions, but this will be a lot harder than a few simple Splits.

Related

How to decode an encoded POST data

From a payment provider I get the data as a POST and encoded with
Content-Type: application/x-www-form-urlencoded; charset=utf-8
the issue is that I need to retrieve the data from the
HttpContext.Current.Request.Form
and a simple System.Web.HttpUtility.UrlDecode(context.Request.Form[0])
returns:
"\" transactionRef\"\r\n\r\nd68cafc5d6c8472a893eb0763efca625\r\n------WebKitFormBoundaryKVsW8jF9QdwY35ca\r\nContent-Disposition: form-data; name=\"transactionNumber\"\r\n\r\n10526728\r\n------WebKitFormBoundaryKVsW8jF9QdwY35ca\r\nContent-Disposition: form-data; name=\"orderRef\"\r\n\r\nc7853c2513fd45758b80b95a2dbaf9fa\r\n------WebKitFormBoundaryKVsW8jF9QdwY35ca\r\nContent-Disposition: form-data; name=\"orderId\"\r\n\r\n50043628\r\n------WebKitFormBoundaryKVsW8jF9QdwY35ca\r\nContent-Disposition: form-data; name=\"accountNumber\"\r\n\r\n50250954\r\n------WebKitFormBoundaryKVsW8jF9QdwY35ca\r\nContent-Disposition: form-data; name=\"zzzz\"\r\n\r\nmust be here to get http post to work"
How can I decode the data so I can retrieve each parameter correctly? for example, extract the transactionRef or orderRef?
I did it in a very weird way and I just wonder if there's something easier/failproof that we can use:

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

Logging into a website by code

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

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