我正在尝试创建一个移动游戏应用程序,它将在游戏开始时拥有一个简单的登录系统。我用PHP创建了我的Rest API。它工作正常。我通过C#在Unity中创建了我的简单GUI。我成功完成了Api Key身份验证。现在我想将用户的电子邮件地址和密码插入我的数据库。我想模拟Rest Console Request Payload(Raw Body),这样我就可以简单地传递JSON值并将它们插入到我的数据库中,如:
{
"username" : "daredevil",
"password": 12345
}
我找到了这个链接,但我认为这不是我想要做的。
How to make a HTTP PUT request?
Passing values to a PUT JSON Request in C#
这是我在Login.cs类中的ParseAuthenticate()方法,它连接到我的API:
public void ParseAuthenticate()
{
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(Login.ValidateRemoteCertificate); // verifying ssl used for login with ServicePoint connection management
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
try
{
httpWebRequest = (HttpWebRequest)WebRequest.Create("https://createAccountURL/register"); //making a request to URL
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method= "PUT";
httpWebRequest.Headers.Add ("api-key:"+"12334566778");
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream newStream = httpWebRequest.GetRequestStream ();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
Debug.Log (responseText);
}
}
catch(Exception e)
{
Debug.Log(e);
}
}
这是我的CreateAccountGUI()方法,它获取用户名,密码,确认用户名,确认密码
void CreatAccountGUI()
{
GUI.Box (new Rect(280,120,(Screen.width/4)+200,(Screen.height/4)+250), "Create Account");
GUI.Label(new Rect(390,200,220,23),"Username");
CUser = GUI.TextField(new Rect(390,225,220,23), CUser);
GUI.Label(new Rect(390,255,220,23),"Password");
CPassword = GUI.TextField(new Rect(390,280,220,23), CPassword);
GUI.Label(new Rect(390,310,220,23),"Confirm Username");
ConfirmUser = GUI.TextField(new Rect(390,340,220,23), ConfirmUser);
GUI.Label(new Rect(390,370,220,23),"Confirm Password");
ConfirmPass = GUI.TextField(new Rect(390,400,220,23), ConfirmPass);
if(GUI.Button(new Rect(370,460,120,25), "Create Account"))
{
if(ConfirmPass == CPassword && ConfirmUser == CUser)
{
StartCoroutine(CreateAccount ());
}
else
{
StartCoroutine(LoginAccount());
}
}
if(GUI.Button(new Rect(520,460,120,25),"Back"))
{
CurrentMenu = "Login";
}
}//End CreateAccountGUI
这是我的IEnumerator CreateAccount()方法:
IEnumerator CreateAccount()
{
Debug.Log ("Button Pressed");
WWWForm form = new WWWForm ();
form.AddField ("Email", CUser);
form.AddField ("Password", CPassword);
WWW CreateAccountWWW = new WWW (CreateAccountUrl);
//wait for php to send something back to Unity
yield return CreateAccountWWW;
if (CreateAccountWWW.error != null)
{
Debug.LogError ("Cannot connect to Account Creation");
}
else
{
string CreateAccountReturn = CreateAccountWWW.text;
if (CreateAccountReturn == "Success")
{
Debug.Log ("Success: Account Created!");
CurrentMenu = "Login";
}
}
}//End CreateAccount
当我运行Unity时,我得到了“无法连接到帐户创建”。任何人都可以帮助我使用这种PUT方法吗?