我正在尝试学习C#/ Silverlight / Windows Phone7。这是怎么回事:当我尝试直接使用MS的MSDN站点上的示例时,遇到各种错误:
例如:
using System;
using System.IO;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Device.Location;
using Microsoft.Phone.Reactive;
private void registerPhone(object sender, RoutedEventArgs e)
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
告诉我
1)
The name 'Encoding' does not exist in this context
2)
System.Net.WebRequest does not contain a definition for GetStreamRequest and no extension method GetRequestStream accepting a first argument of type System.Net.WebRequest could be found (are you missing a using directive... etc
3)ContentLength的类似消息
4)GetResponse的类似消息
我不知道我需要“使用”哪些库,即使当我认为我在“使用”正确的库时,也会出现错误。我究竟做错了什么?
最佳答案
Encoding
类位于System.Text
命名空间中。您必须将其添加到“用法”中。
至于其他消息,这与Silverlight的局限性有关。我认为,MSDN示例是从.NET Framework的“大”版本中获得的,不是吗?
在Silverlight中,您看到,为了防止不良的应用程序行为(例如阻止调用),不允许某些事情,并且由于资源限制,不支持某些事情。
特别地,ContentLength
是后者之一。该库将根据您写入请求中的实际数据量来确定内容长度。只需删除该行,您就可以了。
但是GetResponseStream()
实际上是前者之一。它与以下事实有关:此操作意味着实际上要打开网络连接,这可能需要一段时间。并且由于在Silverlight中不允许阻塞调用,因此GetResponseStream()
方法也必须使用。
相反,您应该使用所谓的“异步模式”-即方法BeginGetResponseStream
/ EndGetResponseStream
对。为此,您可以调用Begin
方法并提供一个回调,一旦操作完成,该回调将被调用。然后,在该回调中,使用End
方法获取操作结果。像这样:
request.BeginGetRequestStream( ar =>
{
var dataStream = request.EndGetRequestStream( ar );
// Write the data to the request stream.
dataStream.Write( byteArray, 0, byteArray.Length );
// Close the Stream object.
dataStream.Close();
// and so on...
}, null );
GetResponse
方法也是如此。关于c# - 在上下文中找不到某些单词,缺少引用,大脑即将爆炸?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5202433/