I'm trying to do a post request on windows phone 8 from the Unity Platform. I do not want to use the unity WWW method as this blocks rendering (and is not thread safe).
The following code works in the editor and on Android, but when building it for WP8 I get the following error.
System.Byte[] System.Net.WebClient::UploadData(System.String,System.String,System.Byte[])` doesn't exist in target framework.
The reason for this error is explained here
It’s because Windows Phone 8 uses a different flavor of .NET called .NET for Windows Phone which is missing some of the types available on other platforms. You’ll have to either replace these types with different ones or implement them yourself. - http://docs.unity3d.com/Manual/wp8-faq.html
This is my code
using (WebClient client = new WebClient())
{
client.Encoding = System.Text.Encoding.UTF8;
client.Headers[HttpRequestHeader.ContentType] = "application/json";
byte[] requestData = new byte[0];
string jsonRequest = "{}";
if (data != null)
{
string tempRequest = Converter.SerializeToString (data);
jsonRequest = "{\"Data\": \"" + tempRequest + "\"}";
requestData = System.Text.Encoding.UTF8.GetBytes(jsonRequest);
}
// below line of code is the culprit
byte[] returnedData = client.UploadData(url, "POST", requestData);
if(returnedData.Length > 0)
{
// do stuff
}
}
I've also tried WebRequests, but GetResponse() breaks it, and HttpClient does not exist.
So, how do I post data in Unity, without using WWW, on windows phone 8?
UPDATE AS PER COMMENT REQUEST - WebRequests
This code, using HttpWebRequest works in the editor and on Android, but on windows phone throws the errors listed below the code.
var request = (System.Net.HttpWebRequest) System.Net.WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "POST";
var sw = new System.IO.StreamWriter(request.GetRequestStream(), System.Text.Encoding.UTF8);
sw.Write(jsonRequest); // jsonRequest is same variable as in above code, string with json object.
sw.Close();
var re = request.GetResponse();
string resultString = "";
using (var outputStream = new System.IO.StreamReader(re.GetResponseStream(), System.Text.Encoding.UTF8))
{
resultString = outputStream.ReadToEnd();
}
if(resultString.Length > 0)
{}
Error 1:
Error: method
System.IO.Stream System.Net.HttpWebRequest::GetRequestStream()
doesn't exist in target framework.
Error 2:
System.Net.WebResponse System.Net.HttpWebRequest::GetResponse()
doesn't exist in target framework.
UPDATE WITH MORE DETAILS - UploadStringAsync
Using this code to make an async request it again works great in the editor, errors are thrown on the WP8.
bool isCompleted = false;
byte[] returnedData = null;
client.UploadDataCompleted +=
new UploadDataCompletedEventHandler((object sender, UploadDataCompletedEventArgs e) =>
{
Debug.Log("return event");
returnedData = e.Result;
isCompleted =true;
});
Debug.Log("async call start");
client.UploadDataAsync(new Uri(url), requestData);
while(isCompleted == false){
Thread.Sleep(100);
}
if(returnedData.Length > 0)
{}
Error 1
method
System.Void System.Net.WebClient::add_UploadDataCompleted(System.Net.UploadDataCompletedEventHandler)
doesn't exist in target framework.
Error 2
Error: method
System.Void System.Net.WebClient::UploadDataAsync(System.Uri,System.Byte[])
doesn't exist in target framework.
Error 3
Error: type
System.Net.UploadDataCompletedEventArgs
doesn't exist in target framework.
Error 4
Error: method
System.Byte[] System.Net.UploadDataCompletedEventArgs::get_Result()
doesn't exist in target framework.
I don't know about any potential restrictions placed on you by Unity, but Windows Phone 8 has the WebClient.UploadStringAsync
method and the WebClient.UploadStringCompleted
event for doing just this.
HttpWebRequest
should also work (again, I don't know about any Unity limitations - see comment above asking for clarification).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With