HTTPWebRequest:重复的帖子

本文关键字:HTTPWebRequest | 更新日期: 2023-09-27 18:32:39

我在下面有以下类来处理HTTP。我是这样使用它的:

   HTTP http = new HTTP();
   string buffer = http.request("http://www.x.com", "username=user&pass=pass");

我正在使用httpAnalyzer来查看数据。有时帖子数据正常:

   username=user&pass=pass

有时它会像这样发布:

   username=user&pass=passusername=user&pass=pass

即使"内容长度"在这两种情况下保持不变。关于为什么它表现得像这样的任何提示?谢谢!

   public partial class HTTP {   
    public static ManualResetEvent allDone = new ManualResetEvent(false);
    const int BUFFER_SIZE = 2048;
    const int DefaultTimeout = 500000;  
    public CookieContainer cookies = new CookieContainer();
    public string GET(string url, string post)
    {
            string buffer = ""; 
            Console.WriteLine(url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.CookieContainer = cookies; request.KeepAlive = true;
            System.Net.ServicePointManager.Expect100Continue = false;
            request.Accept = "*/*";
            request.Headers.Add("Accept-Encoding", "deflate,sdch");
            request.Headers.Add("Accept-Language", "en-US,en;q=0.8");
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
            request.Referer = "https://www.host.com";
            request.KeepAlive = true;
            System.Net.ServicePointManager.Expect100Continue = false;
            RequestState myRequestState = new RequestState();
            AutoResetEvent ev = new AutoResetEvent(false);
            request.KeepAlive = true;
            request.AllowAutoRedirect = true;
            StringBuilder sb = new StringBuilder();
            if (post.Length > 0)
            {
                request.Method = "POST";
                request.ContentLength = post.Length;
                request.CookieContainer = cookies;
                request.ContentType = "application/x-www-form-urlencoded";
                StreamWriter writer = new StreamWriter(request.GetRequestStream());
                writer.Write(post);
                writer.Close();
            }
            ThreadPool.RegisterWaitForSingleObject(ev, new WaitOrTimerCallback(TimeoutCallback), request, DefaultTimeout, true);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            buffer = "";
            buffer = reader.ReadToEnd();
            for (int i = 0; i < response.Headers.Count; i++)
            {
                string name = response.Headers.GetKey(i);
                string value = response.Headers.Get(i);
                if (name == "Set-Cookie")
                {
                    Match match = Regex.Match(value, "(.+?)=(.+?)");
                    string[] tmp = value.Split('=');
                    if (match.Captures.Count > 0)
                    {
                        Uri u = new Uri("https://www.host.com");
                        cookies.SetCookies(u, tmp[0] + "=" + tmp[1]);
                    }
                }
            }
            request.CookieContainer = cookies;
            response.Close();
            reader.Close();
            return buffer;
    }

    public class RequestState
    {
        const int BUFFER_SIZE = 1024;
        public StringBuilder requestData;
        public byte[] BufferRead;
        public HttpWebRequest request;
        public HttpWebResponse response;
        public Stream streamResponse;
        public RequestState()
        {
            BufferRead = new byte[BUFFER_SIZE];
            requestData = new StringBuilder("");
            request = null;
            streamResponse = null;
        }
    }
    private static void TimeoutCallback(object state, bool timedOut)
    {
        if (timedOut)
        {
            HttpWebRequest request = state as HttpWebRequest;
            if (request != null)
            {
                request.Abort();
            }
        }
    }
  }

HTTPWebRequest:重复的帖子

文档提到:

必须调用 Stream.Close 方法来关闭流并释放连接以供重用。无法关闭流会导致应用程序连接不足。

HttpWebRequest重用后台的连接,我可以想象它也重用流并简单地附加数据。如果发生这种情况,以下代码应解决此问题:

using (Stream requestStream = request.GetRequestStream())
using (StreamWriter writer = new StreamWriter(requestStream))
{
    writer.Write(post);
}