HttpWebRequest AsyncCallback参数问题
当我尝试单击第一个方法中的按钮时,它正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将 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?
为什么不能将参数传递给函数?
您使用
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 ofBeginGetResponse
- you can cactually pass any custom object instead of that, holding both your parameters and reference to the request, and to castresult.AsyncState
to that object type instead of casting toHttpWebRequest
.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 getGetRequestStream()
from your request and to write data there (or again as async operation).