I’m using the following code to retrieve a json string from my webserver:
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
}
I intend to encrypt the variables used but would like to be able to post the variables to the server rather than have them included in the query string. How would I achieve this?
Thanks – Stu
edit: Have changed the code to the following:
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
}
as part of the response handling, I check isolatedstorage for a saved file, if it exists then delete it and save the new response into the file.
However, since making the code change above it now won’t let me delete the existing file:
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)
}
...
}
Don’t see why the code change affects this?
edit:
fixed by changing:
StreamReader reader = new StreamReader(e.Result);
string myresult = reader.ReadToEnd();
reader.Close();
reader.Dispose();
to
string myresult = (string)e.Result;
Use upload string to post the data: webClient.UploadStringAsync(uri, “POST”, body);
You just need to configure the ‘body’ data how your server wants to see it.