2

I have this code:

        private HttpWebRequest request;
        private HttpWebResponse wResponse;
        private CookieContainer cookieContainer = new CookieContainer();
        #region PRIVATE METHODS
        private void RunRequest(string url)
        {
            request = HttpWebRequest.Create(new Uri(url)) as HttpWebRequest;
            request.UserAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookieContainer;
            request.Method = "GET";

            StartWebRequest(request);

            //Do smthng
            while (wResponse == null) { }
        }

        private void StartWebRequest(HttpWebRequest request)
        {
            request.BeginGetResponse(FinishWebRequest, request);
        }

        private void FinishWebRequest(IAsyncResult result)
        {
            wResponse = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
        }
        #endregion

And wRequest variable is not filled with any response. In AsyncState I have this: HttpWebRequest error

What could be the problem?

p.s. The same code works good on the desktop-app.

Thanks, Pavel.

korovaisdead
  • 6,271
  • 4
  • 26
  • 33
  • Duplicate of [http://stackoverflow.com/questions/253549/how-do-i-use-httpwebrequest-with-get-method](http://stackoverflow.com/questions/253549/how-do-i-use-httpwebrequest-with-get-method). The basic problem is that GET requests aren't supposed to have a body, which you are specifying with the ContentType. If you remove the ContentType line this should work. – JLaanstra May 19 '12 at 15:41
  • I thought the same, but removing this line, I stopped getting IAsyncResult. Although the internet connection is good and I had to get an answer quickly. – korovaisdead May 19 '12 at 15:49
  • I tried your code and it does work if you remove the `ContentType` and the `//do something` busy loop. Please post a complete example. – TheFogger May 19 '12 at 19:59

2 Answers2

2

There are two problems in your code.

  1. You are not properly using asynchronous callback method:

    Replace

    request.BeginGetResponse(FinishWebRequest, request);

    with

    request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);

  2. Specifying Content-Type for GET request is invalid, it is essential for POST request. Modify RunRequest() method:

    private void RunRequest(string url, string method)
    {
        request = HttpWebRequest.Create(new Uri(url)) as HttpWebRequest;
        request.UserAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16";
        request.Method = method; // method can be GET, POST etc.
        if (method == "POST")
            request.ContentType = "application/x-www-form-urlencoded";
        request.CookieContainer = cookieContainer;
        ...
    }
    
Software Engineer
  • 3,906
  • 1
  • 26
  • 35
  • Your suggestion seems to be correct. But, unfortunately, working as you said, I can never get wResponse. It is always null. What else can I be doing wrong? :( – korovaisdead May 20 '12 at 10:22
  • Do I need to set some permissions for applications that would grant access to the network? – korovaisdead May 20 '12 at 10:25
  • Remove `while (wResponse == null) { }` from `RunRequest()` then place a breakpoint at the next line of `wResponse = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;` to see what it holds. – Software Engineer May 20 '12 at 18:04
  • Nothing. This point does not reach. IAsyncResult is not returned by the BeginGetResponse method and do not passed to FinishWebRequest :( – korovaisdead May 20 '12 at 18:24
  • Also, WebCLient class works well. But I need to use HttpWebRequest here. – korovaisdead May 20 '12 at 18:25
  • 1
    Why do you need to add redundant delegate creation in `BeginGetResponse`? The compiler will generate the exact same code for `new AsyncCallback(FinishWebRequest)` and `FinishWebRequest`. – Cheesebaron May 21 '12 at 12:05
1

There is no reason to specify a ContentType when you are using a GET operation; removing that should solve the problem!

Pedro Lamas
  • 7,185
  • 4
  • 27
  • 35
  • As I said before, after I removed this line, I stopped getting IAsyncResult. But the internet-connection is quite good. – korovaisdead May 19 '12 at 15:53