我有以下响应代码。我收到错误,如System.Windows.ni.dll中发生类型'System.UnauthorizedAccessException'的异常但在用户codeerror中未处理,当调用Messagebox.sohw()方法显示代码时:200。 我的Json就像{“meta”:{“code”:200}}
private void ReadWebRequestCallback(IAsyncResult ar)
{
HttpWebRequest myReq = (HttpWebRequest)ar.AsyncState;
HttpWebResponse myResponse = (HttpWebResponse)myReq.EndGetResponse(ar);
uritext.Text = myResponse.ResponseUri.ToString();
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
string results = httpwebStreamReader.ReadToEnd();
}
myResponse.Close();
var root = JsonConvert.DeserializeObject<RootObject>(jsondata);
String codenum = root.meta.code.ToString();
Messagebox.sohw(codenum);
}
public class Meta
{
public int code { get; set; }
}
public class RootObject
{
public Meta meta { get; set; }
}
答案 0 :(得分:2)
问题是来自HttpWebRequest
的回调是从非UI线程发生的。
以下是在UI线程上执行MessageBox.Show
的操作:
Dispatcher.BeginInvoke(() => MessageBox.Show(codenum));