【问题标题】:GetResponse and GetRequestStream silverlightGetResponse 和 GetRequestStream silverlight
【发布时间】:2012-07-24 07:18:22
【问题描述】:

由于某种原因,我不能在silverlight 中使用GetRequestStreamGetResponse 出现下划线:S 不确定要使用什么?我正在尝试连接到我的网络服务,这是我得到错误的地方,

string uri = "http://localhost:8002/Service/Customer";
StringBuilder sb = new StringBuilder();
sb.Append("<Customer>");
sb.AppendLine("<FirstName>" + this.textBox1.Text + "</FirstName>");
sb.AppendLine("<LastName>" + this.textBox2.Text + "</LastName>");
sb.AppendLine("</Customer>");
string NewCustomer = sb.ToString();
byte[] arr = Encoding.UTF8.GetBytes(NewCustomer );
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/xml";
req.ContentLength = arr.Length;
Stream reqStrm = req.GetRequestStream();// error here GetRequestStream
reqStrm.Write(arr, 0, arr.Length);
reqStrm.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); //error here GetRequestStream
MessageBox.Show("Staff Creation: Status " + resp.StatusDescription);
reqStrm.Close();
resp.Close();

有人有解决方法吗?

【问题讨论】:

    标签: c# asp.net silverlight web-services rest


    【解决方案1】:

    Silverlight 仅支持异步网络访问。 Silverlight 中没有同步的 GetRequestStreamGetResponse 方法。您将需要使用异步方法 BeginGetRequestStream/EndGetRequestStreamBeginGetResponse/EndGetResponse

    更重要的是,您需要了解如何在一般情况下异步执行操作。例如,某些东西会调用您的上述代码,并期望在它完成后会发生某些更改。在不真实的异步世界中,代码会很快返回,稍后会发生一些事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多