WebRequest BeginGetRequestStream 和 BeginGetResponse 不会触发其回调
我有一个使用 JSON 的 PHP Web 服务,我需要从 Silverlight 应用程序向其发送请求。我之前曾在常规 C# 应用程序中使用过 HttpWebRequest 类,并且它运行得非常完美。但是,在我的 Silverlight 应用程序中,不会调用 BeginGetRequestStream
和 BeginGetResponse
方法的回调。
我尝试以各种方式测试它,但没有成功。请注意,我发送的数据并不长 - 大多数时候只是 32 个字符的字符串。我检查了回调是否被调用,但它们没有被调用。这是我的代码:
public void Request(Command cmd, Dictionary<string, string> data)
{
Dictionary<string, object> fullRequest = new Dictionary<string,object>();
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
StringWriter writer = new StringWriter(new StringBuilder());
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, fullRequest);
string query = "query=" + writer.ToString();
request.ContentLength = query.Length;
request.ContentType = "text/json";
_queue.Enqueue(new KeyValuePair<Command, string>(cmd, query));
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
public void GetRequestStreamCallback(IAsyncResult target)
{
HttpWebRequest request = (HttpWebRequest)target.AsyncState;
Stream postStream = request.EndGetRequestStream(target);
string postData = "";
//not sure why I have to do this. in the Silverlight documentation
//Queue<> is supposed to have the SyncRoot property
//but I get an error if I just try _queue.SyncRoot
lock ((_queue as ICollection).SyncRoot)
{
postData = _queue.Dequeue().Value;
}
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
public void GetResponseCallback(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
streamResponse.Close();
streamRead.Close();
response.Close();
JsonSerializer serializer = new JsonSerializer();
Dictionary<string, object> responseData = new Dictionary<string, object>();
responseData = (Dictionary<string, object>)serializer.Deserialize(new StringReader(responseString), typeof(Dictionary<string, object>));
if (_data.ContainsKey(responseData["key"] as string))
{
_data[responseData["key"] as string] = responseData;
}
else
{
_data.Add(responseData["key"] as string, responseData);
}
我也尝试过这个:
IAsyncResult resultRequest = request.BeginGetRequestStream(null, null) as IAsyncResult;
Stream postStream = request.EndGetRequestStream(resultRequest );
同步获取流(是的,风格不好,我只是想看看它是否有效)。没有成功。 提前致谢。
I have a PHP web service using JSON that I need to post requests to from a Silverlight application. I had used the HttpWebRequest
class before, in a regular C# application and it worked flawlessly. In my Silverlight application, however, the callbacks for the BeginGetRequestStream
and BeginGetResponse
methods are not getting called.
I tried testing it out in about every way without success. Note that the data I am sending is not long - just a 32 character string most of the time. I checked if the callbacks were getting called at all, and they weren't being called. Here is my code:
public void Request(Command cmd, Dictionary<string, string> data)
{
Dictionary<string, object> fullRequest = new Dictionary<string,object>();
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
StringWriter writer = new StringWriter(new StringBuilder());
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, fullRequest);
string query = "query=" + writer.ToString();
request.ContentLength = query.Length;
request.ContentType = "text/json";
_queue.Enqueue(new KeyValuePair<Command, string>(cmd, query));
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
public void GetRequestStreamCallback(IAsyncResult target)
{
HttpWebRequest request = (HttpWebRequest)target.AsyncState;
Stream postStream = request.EndGetRequestStream(target);
string postData = "";
//not sure why I have to do this. in the Silverlight documentation
//Queue<> is supposed to have the SyncRoot property
//but I get an error if I just try _queue.SyncRoot
lock ((_queue as ICollection).SyncRoot)
{
postData = _queue.Dequeue().Value;
}
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
public void GetResponseCallback(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
streamResponse.Close();
streamRead.Close();
response.Close();
JsonSerializer serializer = new JsonSerializer();
Dictionary<string, object> responseData = new Dictionary<string, object>();
responseData = (Dictionary<string, object>)serializer.Deserialize(new StringReader(responseString), typeof(Dictionary<string, object>));
if (_data.ContainsKey(responseData["key"] as string))
{
_data[responseData["key"] as string] = responseData;
}
else
{
_data.Add(responseData["key"] as string, responseData);
}
I also tried this:
IAsyncResult resultRequest = request.BeginGetRequestStream(null, null) as IAsyncResult;
Stream postStream = request.EndGetRequestStream(resultRequest );
To get the stream synchronously (yes, bad style, I just wanted to see if it will work). No success .
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论