我正在使用以下代码从我的网络服务器检索 json 字符串:
var webClient = new WebClient();
webClient.OpenReadCompleted += OnOpenReadCompleted;
webClient.OpenReadAsync(new Uri("https://myurl.com/request.cgi?user=" + user + "&pass=" + pass + "&junk=" + DateTime.Now, UriKind.Absolute));
...
private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
// response processed here
}
我打算加密使用的变量,但希望能够将变量发布到服务器而不是将它们包含在查询字符串中。我将如何实现这一目标?
谢谢 - 斯图
编辑:已将代码更改为以下内容:
var webClient = new WebClient();
webClient.UploadStringCompleted += OnOpenReadCompleted;
webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
webClient.Encoding = Encoding.UTF8;
webClient.UploadStringAsync(new Uri("https://myurl.com/request.cgi"), "POST", "user=" + user + "&pass=" + pass + "&junk=" + DateTime.Now, UriKind.Absolute);
...
private void OnOpenReadCompleted(object sender, UploadStringCompletedEventArgs e)
{
// response processed here
}
作为响应处理的一部分,我检查隔离存储以获取已保存的文件,如果存在则将其删除并将新响应保存到文件中。
但是,由于现在更改上面的代码不会让我删除现有文件:
private void OnOpenReadCompleted(object sender, UploadStringCompletedEventArgs e)
{
StreamReader reader = new StreamReader(e.Result);
string myresult = reader.ReadToEnd();
reader.Close();
reader.Dispose();
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
if (store.FileExists("json.txt"))
{
store.DeleteFile("json.txt"); // this errors with System.MethodAccessException was unhandled
// Message=Attempt to access the method failed: System.IO.StreamReader..ctor(System.String)
}
...
}
不明白为什么代码更改会影响这一点?
编辑:
通过更改固定:
StreamReader reader = new StreamReader(e.Result);
string myresult = reader.ReadToEnd();
reader.Close();
reader.Dispose();
到
string myresult = (string)e.Result;