I’m using HttpWebRequest to communicate with a server. The authentication requires cookies in the header.
I have this working well, the next challenge is to do this in the background without blocking the game loop.
I’ve tried multiple suggestions from this thread.
https://stackoverflow.com/questions/202481/how-to-use-httpwebrequest-net-asynchronously
whenever I use
BeginGetResponse() it fails to connect and times out after several seconds - where as the regular GetResponse() works every time.
Looks like someone else had this problem but no solution.
https://stackoverflow.com/questions/51859719/webrequest-getresponseasync-fails-where-webrequest-getresponse-succeeds
As the title says, is it possible in Unity?
The easiest way to do this will always be to “when in Rome do as the Romans do” and use UnityWebRequest.
AFAIK it can do everything you might need network-wise.
Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:
As with all network code, get it working with Postman or curl first. That makes it NOT a Unity problem. Solve the networking first. You must solve that or anything else is irrelevant.
Once you have that working, then try and port it to UnityWebRequest. If it still doesn't work, one handy debug solution is to hook up a proxy like Charles and compare the functioning output from Postman / curl to the output from Unity, and from that reason about where your issues are.
Always get network stuff working outside of Unity in either curl or PostMan, then connect a proxy such as Charles and start trying to replicate the traffic shape with UnityWebRequest.
Any other way risks wasting a lot of your time for trivial issues that only the above way will instantly reveal.
And setting up a proxy can be very helpful too, in order to compare traffic:
yeah thanks, I’ll check it out - I was hoping to make the API class more generic and reusable.
I’m trying to reimplement something with UnityWebRequest that’s working with HttpWebRequest.
I’m currently using…
ServicePointManager.ServerCertificateValidationCallback = TrustCertificate;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = method;
request.AllowAutoRedirect = false;
request.CookieContainer = new CookieContainer();
Cookie csrf = new Cookie("_oauth2_proxy_csrf", CSRF_COOKIE, "/", server_ip);
request.CookieContainer.Add(csrf);
request.Proxy = null;
if (method == "POST")
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("login", username);
nvc.Add("password", password);
StringBuilder postVars = new StringBuilder();
foreach (string key in nvc)
postVars.AppendFormat("{0}={1}&", key, nvc[key]);
postVars.Length -= 1; // clip off the remaining &
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(postVars.ToString());
}
}
var response = (HttpWebResponse)request.GetResponse();
After this login request I successfully receive another cookie “_oauth2_proxy”. This is working great.
Now I’m trying to replicate with UnityWebRequest… but can’t find out what the equivalent CookieContainer is here.
List<IMultipartFormSection> post_data = new List<IMultipartFormSection>();
post_data.Add(new MultipartFormDataSection("login", username));
post_data.Add(new MultipartFormDataSection("password", password));
UnityWebRequest request = UnityWebRequest.Post(url, post_data);
request.uploadHandler.contentType = "application/x-www-form-urlencoded";
request.certificateHandler = new ForceAcceptAll();
request.redirectLimit = 1;
request.SetRequestHeader("_oauth2_proxy_csrf", CSRF_COOKIE);
string cookie = string.Format("{0}={1}", "_oauth2_proxy_csrf", CSRF_COOKIE);
request.SetRequestHeader("Cookie", cookie);
The server doesn’t appear to be to receiving the cookie - as it won’t authenticate correctly and won’t provide “_oauth2_proxy” like it does with HttpWebResponse
Unfortunately I can’t setup a proxy due to the works VPN configuration.
There’s very little documentation on using/handling cookies with UnityWebRequest. I’m a bit stuck.