HttpWebRequest AsyncCallback参数问题

发布于 2024-11-27 02:45:38 字数 1962 浏览 1 评论 0原文

当我尝试单击第一个方法中的按钮时,它正在 for 循环内创建异步 http 请求。但是,我无法将参数传递给异步回调函数。 我想做一件事,我想使用 POST 方法在 for 循环内发送 ID。

    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();
            streamReader.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

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();
            streamReader.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你穿错了嫁妆 2024-12-04 02:45:38

您需要将 ID 作为查询字符串参数添加到 HttpWebRequest(设置后,您不会对 qstr3 执行任何操作)。

当您设置 ContentType 和 ContentLength 时,您不会向其传递任何实际内容。

但令我担心的是(可能)并行发出许多简单的异步 HttpWebRequest。如果它们的数量相当多,我预计糟糕的事情就会开始发生。

考虑一下这是否是正确的方法。 1.php 可以修改为一次获取​​多个 ID 吗?

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?

疑心病 2024-12-04 02:45:38

为什么不能将参数传递给函数?
您使用 request 作为 BeginGetResponse 的第二个参数 - 实际上您可以传递任何自定义对象来代替它,同时保存您的参数和对请求的引用,并进行强制转换result.AsyncState 为该对象类型,而不是转换为 HttpWebRequest

但实际上,如果您需要发送您的 ID,则需要在异步 request.BeginGetResponse 操作之前获取请求流 - 例如从您的请求中获取 GetRequestStream() 并写入那里的数据(或再次作为异步操作)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文