C# HttpWebRequest appends some strange characters in header just before POST characters

Viewed 53

all

I am having some strange errors that I have never experienced before.

Usually I send GET and POST via C# HttpWebRequest.

The code is so simple as below.

HttpWebRequest _req             = (HttpWebRequest)HttpWebRequest.Create(Url);
_req.Method                     = "POST";

var _keyValArray                = PostData
    .Select(s => s.Key
        + "="
        + HttpUtility.UrlEncode(s.Value, Encoding)
        )
    .ToArray();
var _keyValString               = string.Join("&", _keyValArray);
byte[] _postData                = Encoding.GetBytes(_keyValString);

this._SetHeader(ref _req, ref CookieContainer, ref Headers, Constant.ContentType.Application.WwwFormUrlEncoded, Referer, _postData.Length);

using (Stream _sw = _req.GetRequestStream())
{
    _sw.Write(_postData, 0, _postData.Length);
    _sw.Close();
}

using (var _res = (HttpWebResponse)_req.GetResponse())
{
    CookieCollection.Add(_res.Cookies);

    // Get html response
    using (var _sr = new StreamReader(_res.GetResponseStream(), Encoding))
    {
        _html       = _sr.ReadToEnd();
        _sr.Close();
    }
    _contentEncoding    = _res.ContentEncoding;
    _status             = _res.StatusCode;
    _responseUri        = _res.ResponseUri;
    _res.Close();
}

Then this code usually creates http post data as below. enter image description here

Yes, it's fine. No problem at all.

But when I post again to the same url with different body, some strange characters are appended to the post packet as below. enter image description here

This gives me 404 error

I think it is becuase the server cannot recognize the proper protocol method.

Has anyone seen some error like this?

Is this created by the server or my C# code?

Those characters are 9 length and it changes every time.

0 Answers
Related