简体   繁体   English

C#HttpWebRequest使用特殊字符串发布表单数据

[英]C# HttpWebRequest Post form data with special string

I'm trying to post form data to an external url to retrieve a html response. 我正在尝试将表单数据发布到外部URL以检索html响应。 When I try to post these values with a html form element in an .html file, it works fine. 当我尝试在.html文件中使用html表单元素发布这些值时,它可以正常工作。 However, when trying to post the same values with C# HttpWebRequest, it doesn't work. 但是,当尝试使用C#HttpWebRequest发布相同的值时,它不起作用。 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)'>

That is the hidden field in the html form I'm using. 这是我正在使用的html表单中的隐藏字段。 When I send this "id" value as empty or wrong I get an empty string result instead of the expected html. 当我将此“id”值发送为空或错时,我得到一个空字符串结果而不是预期的html。

In C# code, I always get an empty string as response. 在C#代码中,我总是得到一个空字符串作为响应。 This is my C# code: 这是我的C#代码:

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 encoding of this "inputValue" could be related to this, I tried these 2 ways: 我认为这个“inputValue”的编码可能与此有关,我尝试了这两种方式:

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 these worked. 但这些都没有奏效。

Do you have an idea what could be causing this empty string result? 你知道什么可能导致这个空字符串结果吗? Note that in web browser I also get the empty response when the "id" hidden input field has incorrect data. 请注意,在Web浏览器中,当“id”隐藏输入字段包含不正确的数据时,我也会得到空响应。

Could there be a special encoding to be used there with ":::" characters? 是否有一个特殊的编码用于“:::”字符?

Reviewing both calls (one .html file, another c# code) with Fiddler, the difference seems to be the "()" characters: Html file call: 用Fiddler查看两个调用(一个.html文件,另一个c#代码),区别似乎是“()”字符:Html文件调用:

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

C# request sent: 发送C#请求:

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

The encoding is not working for "()" characters? 编码不适用于“()”字符? Any idea it could affect? 知道它会影响吗?

Comparing the 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: C#电话:

 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=

Wrap all your data with System.Web.HttpUtility.UrlEncode. 使用System.Web.HttpUtility.UrlEncode包装所有数据。

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

Q: The encoding is not working for "()" characters? 问:编码不适用于“()”字符?

The encoding is working just as expected. 如预期的编码正是工作。 Encoding.UTF8.GetBytes() is correctly encoding '(' as '%28' and ')' as '%29'` Encoding.UTF8.GetBytes()正确编码'(' '%28'')'为'%29'


Q: Any idea it could affect? 问:有什么想法会影响吗?

It should not have any effect. 不应该有任何影响。 From RFC 1738 , 来自RFC 1738

"Thus, only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL. “因此,只有字母数字,特殊字符”$ -_。+!*'(),“和用于保留目的的保留字符可以在URL中以未编码的方式使用。

On the other hand, characters that are not required to be encoded (including alphanumerics) may be encoded within the scheme-specific part of a URL, as long as they are not being used for a reserved purpose." 另一方面,不需要编码的字符(包括字母数字)可以在URL的方案特定部分内编码,只要它们不用于保留目的即可。


The client side code you've posted looks fine. 您发布的客户端代码看起来很好。 I was able to use your code and successfully pass the string to a 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) 然后我得到responseString为“好”(如预期的那样)

Now why your server is not responding correctly is a separate question and I think you'll need to give details of the server side handling of your request. 现在为什么你的服务器没有正确响应是一个单独的问题,我认为你需要提供服务器端处理请求的详细信息。

Also, you are focusing on the inputValue string but are you sure the other parts of that strNew string are not a problem (for instance, what's the it string)? 此外,你专注于inputValue字符串,但你确定的其他部分strNew串都不是问题(例如,什么是it的字符串)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM