- Home /
Is it possible to either Clone or Re-Send a UnityWebRequest?
We've recently started using UnityWebRequests and something that it is lacking is some way to dictate how many retries it should do before failure, and a manual timeout timer.
No big deal, we'll just wrap it and have our own retries and timeout timer.
Except.... There is no way to Send a UnityWebRequest twice. That sucks, I guess we'll just allocate memory a bunch of times for retries. Fine. Except... There is no constructor that lets you copy one request's info into another.
Any suggestions that don't involve manually copying one request's data into another?
Check out this link - How can I send and receive data to and from a URL, i.e. server side scripts, web services, etc?
Hope this may help you. Nsks
Thanks for the reply but that thread is unrelated. I am using UnityWebRequest, not WWW. I am successfully using UnityWebRequest to perform all HTTP requests. $$anonymous$$y question is about saving on allocations, removing ugly/bad code, and reducing the amount of work we have to do to add basic functionality around the UnityWebRequest.
Answer by Baltrisch · Jun 14, 2019 at 02:32 PM
i know it is 2 years later but, i didn't find other post about it. i didn't find a easier solution but came up with this:
public struct sRequest {
UnityWebRequest request;
string url;
WWWForm form;
public sRequest(string url, WWWForm form) {
this.url = url;
this.form = form;
request = null;
}
private IEnumerator Send() {
request = UnityWebRequest.Post(url, form);
yield return request.SendWebRequest();
if(request.error != null) yield return Send();
}
}
This sends the request indefinitely, but you get the point.
It's a bad idea to execute the coroutine recursively especially there's no reason to do that. Each coroutine will allocate a new coroutine and IEnumerator object. Since you build a chain of these objects none will be released until you break the recursion. You can simply do:
private IEnumerator Send()
{
int count = 0;
while (count < 10)
{
count++;
request = UnityWebRequest.Post(url, form);
yield return request.SendWebRequest();
if(request.error == null)
{
// success
break;
}
}
// Error, no success after 10 tries
}
Though I guess the OP wants to duplicate / reuse the webrequest because it's probably a bit more complex than just a form. $$anonymous$$aybe some custom request headers. Anyways all you have to do is store any relevant data inside that request class (which really should be a class and not a struct).
Your answer
Follow this Question
Related Questions
UnityWebRequest seems to crash application when active on Application.Quit()? 1 Answer
How an Online Server Game Works? 1 Answer
Check if there is a host on a specific IP? 0 Answers
[Unet] Player object loses authority on scene switch 1 Answer
How to destroy a clone of an effect attached to an object? 1 Answer