我为一个程序做了一个函数,当请求类型为 GET 时它确实工作,如果它是 POST,它总是在 0x2518122413433433433
我尝试了很多东西,但我没有找到原因,这里有人知道吗。

编辑:开始工作,如果有人感兴趣:https://gist.github.com/4347248

任何帮助将不胜感激。

我的代码是:

public ResRequest request(string URL, RequestType typ, CookieCollection cookies, string postdata ="", int timeout= 50000)
    {
        byte[] data;
        Stream req;
        Stream resp;

        HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
        request.Timeout = timeout;
        request.ContinueTimeout = timeout;
        request.ReadWriteTimeout = timeout;
        request.Proxy = new WebProxy("127.0.0.1", 8118);
        request.Headers.Add(HttpRequestHeader.AcceptLanguage, "de");
        request.Headers.Add("UA-CPU", "x86");


        request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618) ";
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(cookies);
        if (typ == RequestType.POST)
        {
            data = System.Text.Encoding.Default.GetBytes(postdata);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            req = request.GetRequestStream();//after a few tries this produced a Timeout error
            req.Write(data, 0, data.Length);
            req.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();//This line produces a Timeout Exception
            resp = response.GetResponseStream();

            if ((response.ContentEncoding.ToLower().Contains("gzip")))
            {
                resp = new System.IO.Compression.GZipStream(resp, System.IO.Compression.CompressionMode.Decompress);
            } else if ((response.ContentEncoding.ToLower().Contains("deflate"))) {
                resp = new System.IO.Compression.DeflateStream(resp, System.IO.Compression.CompressionMode.Decompress);
            }

            return new ResRequest() { result = new System.IO.StreamReader(resp, System.Text.Encoding.UTF8).ReadToEnd(), cookies = response.Cookies, cstring = cookiestring(response.Cookies) };

        }
        else
        {
            request.Method = "GET";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            resp = response.GetResponseStream();

            if ((response.ContentEncoding.ToLower().Contains("gzip")))
            {
                resp = new System.IO.Compression.GZipStream(resp, System.IO.Compression.CompressionMode.Decompress);
            }
            else if ((response.ContentEncoding.ToLower().Contains("deflate")))
            {
                resp = new System.IO.Compression.DeflateStream(resp, System.IO.Compression.CompressionMode.Decompress);
            }

            return new ResRequest() { result = new System.IO.StreamReader(resp, System.Text.Encoding.UTF8).ReadToEnd(), cookies = response.Cookies, cstring = cookiestring(response.Cookies) };
        }

    }

最佳答案

那么它是每次都卡在 req.GetRequestStream() 上,还是“尝试几次”然后挂起?

如果它运行几次然后挂起,则可能是您没有正确关闭请求,这导致您的连接用完。确保 Close() 和/或 Dispose() HttpWebResponse 对象以及您正在创建的所有流和读取器。

关于c# - request.GetResponse 总是给出超时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13955306/

10-13 08:08