Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends:

I meet a question when log in a server with HttpWebRequest post mode. I find out two abnormal point. one is that Cookie is empty, the other is that the response is unreadable code, but the this.response.StatusCode is equal to OK.

How resolve the problem, thank in advance!!!

Code is below:
C#
public string BeginPostSend(string posturl, 
    Encoding encoding,
    string UserAgent,
    string Accept,
    string AcceptLanguage,
    string proxyURL,
    string postData)
{
    string contentResult = string.Empty;
    string err = string.Empty;
    int couterTry = 0;
          
    byte[] data = encoding.GetBytes(postData);
    char[] charArray = new char[2048];

    do
    {
        // Set some request parameter
        // So as to readability, don't put below parameter set into a lonely function  
        this.request = WebRequest.Create(posturl) as HttpWebRequest;

        this.request.CookieContainer = this.cookieContainer;
        this.request.AllowAutoRedirect = true;
        this.request.Method = "POST";
        this.request.Accept = Accept;
        this.request.Headers.Add("Accept-Language", AcceptLanguage);
        this.request.Headers.Add("Accept-Encoding", "gzip, deflate");
        this.request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
        this.request.KeepAlive = true;
        this.request.ContentLength = data.Length;
        this.request.ContentType = "application/x-www-form-urlencoded";
        this.request.UserAgent = UserAgent;
        this.request.Timeout = 60000;

        //Define a Proxy Object
        WebProxy proxy = new WebProxy();
        //Server address and port                          
        proxy.Address = new Uri(proxyURL);
        //User and Password                         
        //proxy.Credentials = new NetworkCredential("", "");
        //Launch web Authentication
        this.request.UseDefaultCredentials = true;
        //Set proxy                
        this.request.Proxy = proxy;

        try
        {
            couterTry++;

            if (couterTry > 1)
            {
                Thread.Sleep(6000);
            }

            // Put post data into stream
            this.outStream = request.GetRequestStream();
            this.outStream.Write(data, 0, data.Length);

            // Send request and get relative response data
            using (this.response = request.GetResponse() as HttpWebResponse)
                using (this.inStream = this.response.GetResponseStream())
                    using (this.SR = new StreamReader(this.inStream, encoding))
                    // Not untill request.GetResponse(), program begin sending Post request to destination Web page 
                    {
                        if (this.response.StatusCode == HttpStatusCode.OK)
                        {
                            streamDelegate SRDelegate = new streamDelegate(this.SR.ReadToEnd);

                            try
                            {
                                IAsyncResult asyncR = SRDelegate.BeginInvoke(null, null);
                                asyncR.AsyncWaitHandle.WaitOne();
                                contentResult = SRDelegate.EndInvoke(asyncR);
                            }
                            catch 
                            {
                                this.DisposeStream();
                                continue;
                            }

                            //contentResult = tempResult.ToString();
                            this.DisposeStream();
                            break;
                        }
                        else
                        {
                            this.DisposeStream();
                            contentResult = string.Empty;

                            if (couterTry >= 10)
                            {
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.DisposeStream();
                    contentResult = string.Empty;
                    err = ex.Message;

                    if (couterTry >= 10)
                    {
                        break;
                    }
                }

            } while (true);

            this.DisposeStream();

            return contentResult;
        }
Posted
Updated 22-May-14 3:51am
v2
Comments
Prasad Khandekar 22-May-14 9:56am    
Hello Aaron,

The code might not be setting any cookies and hence on the server log no cookie information is available, since you are getting response status as 200 OK, there is no problem on the server side, perhaps the response is gzipped as your code is indicating to server that it can accept gzipped encoded response.

Regards,
Aaron Bo 23-May-14 4:28am    
Hi Prasad:
The first, thank you very much!
I have set head parameter "this.request.Headers.Add("Accept-Encoding", "gzip, deflate");", is this your prompt? Do I need set others head parameter?

Thanks!
BR,
Bo

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900