Reputation: 43
I'm familiar with Unity but new to trying to communicate with servers. I'm trying to set up a login screen but I'm having troubling Posting to the server properly. The strange part is the Get is working fine, but the Post turns up the following Error :
Cannot connect to destination host Network
UnityEngine.Debug:Log(Object)
LoginHandler:CheckForNetworkErrors(UnityWebRequest) (at
Assets/Scripts/LoginHandler.cs:120)
<LoginUser>c__Iterator2:MoveNext() (at Assets/Scripts/LoginHandler.cs:92)
.UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
I'm using a test URL to make sure it was't an issue with the original. Both return the same error. The following code is sent once the player hits a login button. Any ideas on what im doing wrong here?
public IEnumerator LoginUser()
{
string testURL = "https://www.google.com/";
using (UnityWebRequest get = UnityWebRequest.Get(testURL))
{
yield return get.Send();
ParseCSRF(get.downloadHandler.text);
CheckForNetworkErrors(get);
}
WWWForm form = new WWWForm();
form.AddField("username", username.text);
form.AddField("password", password.text);
form.AddField("_csrf", csrf);
using (UnityWebRequest post = UnityWebRequest.Post(WWW.EscapeURL(testURL), form))
{
yield return post.SendWebRequest();
CheckForNetworkErrors(post);
}
}
public void CheckForNetworkErrors(UnityWebRequest www)
{
if(www.isNetworkError)
{
Debug.Log(www.error + " Network");
}
else if (www.isHttpError)
{
Debug.Log(www.error + " http");
}
else
{
Debug.Log("Form upload complete!" + www.downloadHandler.text);
}
}
Upvotes: 2
Views: 9933
Reputation: 545
I have tested with the code I wrote below:
void Start()
{
StartCoroutine(GetCrt());
}
IEnumerator GetCrt()
{
string testURL = "https://www.google.com/";
using (UnityWebRequest www = UnityWebRequest.Get(testURL))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Get Request Completed!");
}
}
}
It's working fine.
For the post request you need a real form data, you can't send a post request to google.com.
Hope this help you.
Happy Coding!
Upvotes: 2