开发者

HttpWebRequest AsyncCallback parameter problem

When I attempt to click to button in the first method,it's creating asynchronous http request inside for loop.But,I can't pass the parameter to my asynchronous callback function. I wanna do thing,I want to send IDs inside for loop by using POST method.

    void Button3Click(object sender, EventArgs e)
    {            
        for(int i = Convert.ToInt32(startno3.Text); i<Convert.ToInt32(endno3.Text); i++) {                
            ASCIIEncoding encoding=new ASCIIEncoding();
            string postData="id=1";                
            qstr3 = encoding.GetBytes(postData);                

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/ornek/1.php");
            if(key1.Text!="") {
                request.Headers.Add("Cookie", "PHPSESSID=" + key1.Text);                    
            }
            request.Method = "POST";
            request.ContentType="application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            IAsyncResult asyncResult = request.BeginGetResponse( new AsyncCallback(EndScanFeeds), request);    
        }
    }

    public void EndScanFeeds(IAsyncResult result) {
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        Stream stream = null;
        StreamReader streamReader = null;
        try {
            request = (HttpWebRequest)result.AsyncState;
            response = (HttpWebResponse)request.EndGetResponse(result);
            stream = response.GetResponseStream();
            streamReader = new StreamReader(stream);
            string feedData = streamReader.ReadToEnd();
            response.Close();
            stream.Close();
            stre开发者_如何学GoamReader.Close();
            MessageBox.Show(feedData);

        }
        catch(Exception ex) {
            throw(ex);
        }
        finally {
            if(response != null)
                response.Close();
            if(stream != null)
                stream.Close();
            if(streamReader != null)
                streamReader.Close();
        }
    }   c


You need to add the ID as a query string parameter to the HttpWebRequest (you're not doing anything with qstr3 after having set it).

And while you're setting ContentType and ContentLength, you're not passing it any actual content.

But what worries me is firing off (potentially) many simple asynchronous HttpWebRequests in parallel. If there's a significant number of them, I'd expect bad things to start happening.

Consider if it's the right approach. Can 1.php be modified to take more that one ID at a time?


Why you can't pass parameters to your function ? You're using request as the second parameter of BeginGetResponse - you can cactually pass any custom object instead of that, holding both your parameters and reference to the request, and to cast result.AsyncState to that object type instead of casting to HttpWebRequest.

But actually if you neeed to send your id's you need to get request stream before your async request.BeginGetResponse operation - for instance to get GetRequestStream() from your request and to write data there (or again as async operation).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜