C # HttpWebRequest Post form data with custom string

I am trying to submit form data to an external url to get a html response. When I try to post these values โ€‹โ€‹using the html form element in the .html file, it works fine. However, when trying to post the same values โ€‹โ€‹from the C # HttpWebRequest, it won't work. The problematic variable is this:

 <input type="hidden" name="id" id="id" value='2015-08-19 13:30:00:::2015-08-19 16:40:00:::Estarder 1:::V:::VAI:::AAA:::460:::02:10:00:::CRJ:::V,Y:::::::::Z8:::Santa Cruz (VVI):::Salta(SLA)'>

      

This is a hidden field in the html form I'm using. When I submit this "id" value as empty or incorrect, I get an empty string result instead of the expected html.

In C # code, I always get an empty string as a response. This is my C # code:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.anywebsite.com/reser/server/actu_status.php");
            request.Method = "POST";
            request.AllowAutoRedirect = false;
            request.CookieContainer = cookies;
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.ContentType = "application/x-www-form-urlencoded";

            string strNew = "&it=" + it + "&tipo_new=1&id=" + inputValue + "&monto=0&mon=";

            var data = Encoding.UTF8.GetBytes(strNew);

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var response = (HttpWebResponse)request.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

      

I think the encoding of this "inputValue" might be related to this, I tried these two ways:

1)  string strNew = "&it=" + it + "&tipo_new=1&id=" + inputValue + "&monto=0&mon=";

2)   string strNew = "&it=" + it + "&tipo_new=1&id=" + HttpUtility.UrlEncode(inputValue) + "&monto=0&mon=";

      

But none of them worked.

Do you have any idea what might be causing this empty result? Note that in the web browser I also get an empty response when the hidden "id" input field contains invalid data.

Could there be a special encoding to be used with ":" characters there?

Revisiting both calls (one .html file, another C # code) with Fiddler, the difference seems to be the "()" character: Html file call:

 3a%3a%3aSanta+Cruz+(VVI)%3a%3a%3aSalta(SLA)&monto=0&mon=

      

C # request sent:

 3A%3ASanta+Cruz+%28VVI%29%3A%3A%3ASalta%28SLA%29&monto=0&mon=

      

Encoding not working for characters "()"? Any idea how this might affect?

Comparison of headers:

POST https://www.anysite.com/reserva3/server/actu_estado.php HTTP/1.1
Host: www.anysite.com
Connection: keep-alive
Content-Length: 380
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: null
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Accept-Language: es-ES,es;q=0.8,en;q=0.6
Cookie: WebAmaszonas=12a620133f447d1fd126c3bc176eb411; 20011920910-20150729143408-157141320855b91ca0074937-00865209=d433a027559697ffab682c23d799b0a6; 20011920910-20150729145250-68716839655b921024d6852-68684734=236496c8c7338b2f627a8ef232e594ad; 20011920910-20150729145319-101832047555b9211fbac1b4-84013043=462e294432435993d6abd781afedbe83; 20011920910-20150729145353-64952403855b92141797c34-27814538=a53da15223e0aa3738f11287b3e865ba; 20011920910-20150729145413-186346569255b921550c42a1-25690349=761611e26d9a55fd942f9ef096a3f082; 20011920910-20150729151718-96356722455b926bed246b0-24253581=e982ad1989a3d8435c6ae9e5fe19becc; 20011920910-20150729152154-11483789755b927d29a89c7-69865088=d5db38b9fdc3129fb272d2443375cf05; 20011920910-20150729152639-202823217355b928efe950f6-26854157=ca2f211d604ed43e1241d60c6542eb45; 20011920910-20150729152721-101593653455b929195ff4e0-55606804=6fabc92e9a3055fd4ac093cc33279d06; 20011920910-20150729153742-188056231255b92b8686f940-62606835=2e96f159b24b5b530b8985c1633a7534; 20011920910-20150729164914-64775098455b93c4a72d2e1-66426174=c585a55b2be8a312999c204f2591182a; 20011920910-20150729174029-113315944155b9484d134e05-75659750=224ec629dc27eee2287407578698ecff; 20011920910-20150729175035-123576351955b94aabac5d80-96847521=9d49f53a42c274351a9ea08856fec884; 20011920910-20150730110547-80615724155ba3d4b1ddde5-79868945=3e367e93767d39dae60037815ce5dc98; 20011920910-20150730110846-190316769455ba3dfe9ae3b8-00355181=cc2757b9bfd61f8da604057d098365c4; 20011920910-20150730120640-153925119555ba4b905daad6-58863984=fbf9b9189f8eb5c8c699353811206009; 20011920910-20150730155822-106183958655ba81dea52c27-39061829=955ac586630dacfd7958a2c945b517b3; __utma=44289076.1245823098.1438112030.1438271798.1438352283.5; __utmc=44289076; __utmz=44289076.1438271798.4.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); 20011920910-20150731101839-161050305155bb83bfc580f6-88478058=f4f290a7c96f10d6a251cc76c863faeb; 20011920910-20150731101913-144151518355bb83e1e874b3-12891534=8cd2dfa83c7bb6a4a8e9e9f26cb1e08e; 20011920910-20150731101951-195196827255bb8407a66238-65768491=9788783fa71200d225307d55585675d3; 20011920910-20150731102004-27173017555bb8414516340-71888646=820389de93795e6c94efabead492d845; 20011920910-20150731102028-97871570555bb842c84a2a0-94655428=2498720cc1ac2ee970c5e7e10952562c; 20011920910-20150731104252-143815792155bb896c959082-24175470=61bd70cec2d70aa254e099f7a718c5ff; 20011920910-20150731105957-195345078055bb8d6d7f2078-02814471=c5c0b192d70d36cd8303267f2bce8925; 20011920910-20150731104252-143815792155bb896c959082-24175470a=f70bc817d6362383b9bc1fa240396fe1; 20011920910-20150731133128-214572938755bbb0f04252a9-82382000=dd4dfbb4a1e3ea0e56e35256308ce08e; 20011920910-20150731152546-201600117255bbcbba049567-54330748=7bae974ce7eb9325188f333fb1812b5f

 it=20011920910-20150731152546-201600117255bbcbba049567-54330748&tipo_new=1&id=11%3A30%3A00%3A%3A%3A16%3A40%3A00%3A%3A%3A2015-08-19+13%3A30%3A00%3A%3A%3A2015-08-19+16%3A40%3A00%3A%3A%3AEscalas+1%3A%3A%3AV%3A%3A%3AVVI%3A%3A%3ASLA%3A%3A%3A460%3A%3A%3A02%3A10%3A00%3A%3A%3ACRJ%3A%3A%3AV%2CY%3A%3A%3A%3A%3A%3A%3A%3A%3AZ8%3A%3A%3ASanta+Cruz+%28VVI%29%3A%3A%3ASalta%28SLA%29&monto=0&mon=

      

C # call:

 POST https://www.anysite.com/reservar/server/actu_estado.php HTTP/1.1
 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
 Content-Type: application/x-www-form-urlencoded
 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36
 Accept-Encoding: gzip, deflate
 Upgrade-Insecure-Requests: 1
 Cache-Control: max-age=0
 Origin:  
 Accept-Language: es-ES,es;q=0.8,en;q=0.6
 Host: www.anysite.com
 Cookie: 20011920910-20150731161313-197603319955bbd6d90fbb13-41505311=4e901d35a856baef9b00ed3500d89113
 Content-Length: 384
 Connection: Keep-Alive

 it=20011920910-20150731161313-197603319955bbd6d90fbb13-41505311&tipo_new=1&id=13%3a30%3a00%3a%3a%3a16%3a40%3a00%3a%3a%3a2015-08-14+13%3a30%3a00%3a%3a%3a2015-08-14+16%3a40%3a00%3a%3a%3aEscalas+1%3a%3a%3aV%3a%3a%3aVVI%3a%3a%3aSLA%3a%3a%3a460%3a%3a%3a02%3a10%3a00%3a%3a%3aCRJ%3a%3a%3aH%2cV%2cY%3a%3a%3a%3a%3a%3a%3a%3a%3aZ8%3a%3a%3aSanta+Cruz+%28VVI%29%3a%3a%3aSalta%28SLA%29&monto=0&mon=

      

+3


source to share


2 answers


Wrap all your data with System.Web.HttpUtility.UrlEncode.



string strNew = "&it=" + it + "&tipo_new=1&id=" + inputValue + "&monto=0&mon=";
strNew = System.Web.HttpUtility.UrlEncode(strNew);

      

0


source


Q: Encoding does not work for characters "()"

Encoding works as expected. Encoding.UTF8.GetBytes()

correctly encodes '('

how '%28'

and ')'

how '% 29'


Q: Any idea that might be influenced by this?

It shouldn't have any effect. From RFC 1738 ,

"Thus, only alphanumeric characters, special characters" $ -_. +! * '(), "and reserved characters used for their reserved purposes can be used unencoded in the URL.

On the other hand, characters that are not required for encoding (including alphanumeric) can be encoded within the scheme part of the URL if they are not used for the reserved target. "




The client side code you posted looks great. I was able to use your code and successfully transfer the string to the server. So on the server side, if I do the following,

var httpRequest = HttpContext.Current.Request;
string inputString = "2015-08-19 13:30:00:::2015-08-19 16:40:00:::Estarder 1:::V:::VAI:::AAA:::460:::02:10:00:::CRJ:::V,Y:::::::::Z8:::Santa Cruz (VVI):::Salta(SLA)";
if (httpRequest.Form["id"] == inputString)
  return Ok("Good");

      

Then I get responseString

as "Good" (as expected)

Now why your server is not responding correctly is a separate question and I think you will need to provide details of the server side processing of your request.

Also, are you focusing on a line inputValue

, but are you sure that other parts of that line strNew

are not the problem (for example, what is the line it

)?

0


source







All Articles