PUT
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://120.78.72.125:28088/usercenter/users-anon/password?newPassword=" + newPass + "&verifiedCode="+ passCode);
request.ContentType = "text/html:charset=UTF-8";
request.Method = "PUT";
request.UserAgent = null;
request.Timeout = 5000;
//using (var streamWriter = new StreamWriter(request.GetRequestStream()))
//{
// string json = "{" +
// "'newPassword': '"+ newPass + "'," +
// "'verifiedCode': '" + passCode + "'," +
// "}";
// streamWriter.Write(json);
//}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Debug.Log(retString);
MsgData reData = JsonMapper.ToObject<MsgData>(retString);
errorTextTip.text = reData.responseDescription;
StartCoroutine(CloseErrorTip());
if (reData.statusCode == 200)
{
gameObject.SetActive(false);
}
else
{
errorTextTip.text = "验证失败";
StartCoroutine(CloseErrorTip());
}
GET
string strPath = "http://120.78.72.125:28088/usercenter/users-anon/forget-password?userName=" + phoneNumber + "&userType=PHONE";
#region
UnityWebRequest request = new UnityWebRequest(strPath);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
Debug.Log("Status Code: " + request.responseCode);
if (request.isNetworkError || request.isHttpError)
{
Debug.Log("链接服务器异常:" + request.error);
}
else
{
//执行倒计时
Debug.Log(request.downloadHandler.text + "PPPPPPPPPPPPPPPPPPPPPPPPPPPP");
CodeData root = JsonMapper.ToObject<CodeData>(request.downloadHandler.text);
Debug.Log(root.statusCode + "PPPPPPPPPPPPPPPPPPPPPPPPPPPP");
if (root.statusCode == 200)
{
errorTextTip.text = "发送验证码成功";
StartCoroutine(CloseErrorTip());
isOpenTimer = true;
btn_Send.enabled = false;
}
else
{
errorTextTip.text = "发送验证码失败";
StartCoroutine(CloseErrorTip());
}
POST
Dictionary<string, string> headers = new Dictionary<string, string>();
headers["Content-Type"] = "application/json";
string strPath = "http://120.78.72.125:28088/usercenter/users-anon/register";
string randomCode = passCode.text;
int terminalKindId = 1;
string userPwd = password.text;
string userType = "PHONE";
string username = phone.text;
Debug.Log("<color=red>"+randomCode+"</color>");
Debug.Log("<color=green>" + terminalKindId + "</color>");
Debug.Log("<color=blue>" + userPwd + "</color>");
Debug.Log("<color=red>" + userType + "</color>");
Debug.Log("<color=green>" + username + "</color>");
string strJson = "{\"randomCode\":\""+ randomCode + "\",\"terminalKindId\":\"" + terminalKindId + "\",\"userPwd\":\""+ userPwd +"\",\"userType\":\""+userType+ "\",\"username\":\"" + username + "\"}";
WWW www = new WWW(strPath, System.Text.Encoding.Default.GetBytes(strJson), headers);
yield return www;
if (www.isDone)
{
//RegisterData
//{"statusCode":410,"responseDescription":"注册用户不成功,请稍后再试","innerException":""}
Debug.LogError("收到服务器回复++++++++++++++++++++++");
print(www.text);
RegisterData reData = JsonMapper.ToObject<RegisterData>(www.text);
if (reData.statusCode == 200)
{
errorTextTip.text = "注册成功";
StartCoroutine(CloseErrorTip());
Debug.Log("注册成功跳转登录界面");
gameObject.SetActive(false);
}
else
{
//显示一个提示框
Debug.Log("错误描述"+reData.statusCode);
errorTextTip.text = "注册失败";
StartCoroutine(CloseErrorTip());
}
Json解析格式
public class RegisterData
{
/// <summary>
public int statusCode { get; set; }
/// <summary>
public string responseDescription { get; set; }
/// <summary>
public string innerException { get; set; }
}
public class CodeData
{
public int statusCode { get; set; }
/// <summary>
///
/// </summary>
public string responseDescription { get; set; }
/// <summary>
///
/// </summary>
public string innerException { get; set; }
public string check { get; set; }
/// <summary>
///
/// </summary>
public string mobile { get; set; }
/// <summary>
///
/// </summary>
public string registered { get; set; }
}
public class MsgData
{
/// <summary>
public int statusCode { get; set; }
/// <summary>
public string responseDescription { get; set; }
/// <summary>
public string innerException { get; set; }
}