我正在使用Unity WWWForm将许可证验证请求发布到URL。 该代码在Unity 5.6中运行,但在Unity 2017.3.1f1中不运行。我也在Unity 2018中尝试过。它没有用。
这是错误消息: 遇到无效的重定向(缺少Location标头?)
这是我正在使用的代码。
void Awake() {
Instance = this;
WWWForm form = new WWWForm ();
mainHeader = form.headers;
mainHeader ["Authorization"] = "Basic " + System.Convert.ToBase64String (System.Text.Encoding.ASCII.GetBytes ("dummyId:#dummyPassword"));
}
public void HitForLicence(){
string jsonData = JsonUtility.ToJson (new LicenseData {
LICENCE_KEY="kwsnfdksfksnf",
MACHINE_IP="192.168.1.1"
});
Debug.Log (jsonData);
byte[ ] postData = System.Text.Encoding.ASCII.GetBytes (jsonData);
//headers ["Authorization"] = "Basic " + System.Convert.ToBase64String (System.Text.Encoding.ASCII.GetBytes ("unity:@piun!ty"));
if (mainHeader.ContainsKey ("Content-Type")) {
mainHeader ["Content-Type"] = "application/json";
} else {
mainHeader.Add ("Content-Type", "application/json");
}
WWW www = new WWW (LicenseURL, postData, mainHeader);
StartCoroutine (CheckForLicense (www));
}
public IEnumerator CheckForLicense (WWW www)
{
Debug.Log("Check For License..");
yield return www;
//if (www.isDone ) {
if (www.error != null) {
ClearKeyBox ();
print (www.error);
}
else {
print (www.text);
jsonNode = SimpleJSON.JSON.Parse(www.text);
print ("MSG "+ jsonNode["MSG"].ToString());
}
//}
if (jsonNode != null && jsonNode["MSG"].Equals(ValidStr)) {
HandleTextFile.WriteString(_SystemMACAddress+"-"+keyEntered);
// Next screen
} else {
ClearKeyBox ();
}
}
有人遇到过吗?请帮忙。
答案 0 :(得分:0)
在Unity 2018下,我在WWW
上也遇到了一些问题。
当前我正在使用UnityWebRequest
-我认为WWW
已过时,但我找不到对此的任何引用。
基本上像这样更改代码:
...
UnityWebRequest www = UnityWebRequest.Post(LicenseURL, form)
StartCoroutine (CheckForLicense (www));
}
public IEnumerator CheckForLicense (WWW www)
{
Debug.Log("Check For License..");
...
我认为您必须通过SetRequestHeader
设置头像并将帖子数据放入WWWForm ...
我现在无法检查...希望对您有所帮助!
答案 1 :(得分:0)
就像dome12b说的那样,更改为UnityWebRequest。
此外,将分块传输设置为false。我遇到了同样的问题,这让我发疯了。分块传输解决了所有问题。
UnityWebRequest www = UnityWebRequest.Post(LicenseURL, form)
www.chunkedTransfer = false;
答案 2 :(得分:0)
这终于解决了我的问题。
public void Post(string url)
{
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
string jsonData = JsonUtility.ToJson (new LicenseData {
LICENCE_KEY="kwsnfdksfksnf",
MACHINE_IP="192.168.1.1"
});
Debug.Log (jsonData);
byte[ ] postData = System.Text.Encoding.ASCII.GetBytes (jsonData);
if (mainHeader.ContainsKey ("Content-Type")) {
mainHeader ["Content-Type"] = "application/json";
} else {
mainHeader.Add ("Content-Type", "application/json");
}
StartCoroutine (HttpRequest(postData, url));
}
IEnumerator HttpRequest(byte[] postData, string url)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.AllowAutoRedirect = false;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string jsonData = JsonUtility.ToJson(new LicenseData
{
LICENCE_KEY = "kwsnfdksfksnf",
MACHINE_IP = "192.168.1.1"
});
streamWriter.Write(jsonData);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
print(result);
}
yield return null;
}