C#WebClient标题不是同一种类

时间:2015-05-03 21:28:58

标签: c# http-headers http-post webclient

守则:

using (WebClient wc = new WebClient())
            {
                if (Form1.ProxyEnabled)
                {
                    WebProxy wp = new WebProxy(string.Format("{0}:{1}", PAddress, PPort));
                    if (Form1.AuthenRequired)
                        wp.Credentials = new NetworkCredential(PUser, PPass);
                    wc.Proxy = wp;
                }
                ServicePointManager.Expect100Continue = false;

                wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                wc.Headers[HttpRequestHeader.Referer] = "xxxxxxxxxxxxxx";
                wc.Headers[HttpRequestHeader.UserAgent] = ua;
                wc.Headers[HttpRequestHeader.AcceptLanguage] = "en-US,en;q=0.8";
                wc.Headers[HttpRequestHeader.Accept] = "*/*";
                wc.Headers.Add("Custom", "xxxxxxxxxxxxxx");

                Uri URI = new Uri("xxxxxxxxxxxxxx");
                ResultString = wc.UploadString(URI, ss);
            }

结果是:

POST /api.php HTTP/1.1
Accept-Language: en-US,en;q=0.8
Custom: xxxxxxxxxxxxxx
Accept: */*
Content-Type: application/x-www-form-urlencoded
Referer: xxxxxxxxxxxxxx
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Host: xxxxxxxxxxxxxx
Content-Length: xxxxxxxxxxxxxx
Connection: Keep-Alive

预期结果是:

POST /api.php HTTP/1.1
    Host: xxxxxxxxxxxxxx
    Content-Type: application/x-www-form-urlencoded
    Referer: xxxxxxxxxxxxxx
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
    Accept-Language: en-US,en;q=0.8
    Accept: */*
    Custom: xxxxxxxxxxxxxx
    Content-Length: xxxxxxxxxxxxxx
    Connection: Keep-Alive

那么,发生了什么?为什么标题与我添加标题的顺序不一样以及如何修复它?如何按照确切的顺序发送自定义标头?

1 个答案:

答案 0 :(得分:0)

我选择了tcpclient,一切都很顺利。