WWWForm HTTP POST And GET (Bad Request / Unauthorised) Issue
I am using WWWForm
for HTTP GET and POST. In My Game URL credentials are added, and data GET and POST as Json
format.
I can get the details using credentials on Browser, But i cannot access details using WWWForm
. First I was facing 401 Unauthorised problem. Then i added header like
Dictionary<string, string> headers = form.headers;
headers["content-type"] = "application/json";
headers["Authorization"] = "Basic " + System.Convert.ToBase64String(Encoding.Default.GetBytes(username+":"+password));
After that i don't have getting 401 Unauthorised problem . Know I have getting 400 Bad Request
Here is my Code.
IEnumerator ConnectToServer2(string username,string password)
{
WWWForm form = new WWWForm();
Dictionary<string, string> headers = form.headers;
headers["content-type"] = "application/json";
headers["Authorization"] = "Basic " + System.Convert.ToBase64String(Encoding.Default.GetBytes(username+":"+password));
// form.AddField( "Book_Name", "Night%20at%20call%20centre" );
form.AddField( "Book_Name", "Night at call centre" );
form.AddField( "Book_Part", "First" );
form.AddField( "Sequence", "1" );
// form.AddField( "username", username );
// form.AddField( "password", password );
byte[] rawData = form.data;
// Post a request to an URL with our custom headers
WWW www = new WWW(bookViewUrl, rawData, headers);
yield return www;
if (www.error != null) {
Debug.Log (www.error);
} else {
Debug.Log (www.text);
}
//.. process results from WWW request here...
}
Thanks in advance.
Answer by liju_techtreeit · Dec 18, 2017 at 01:12 PM
IEnumerator LoginToServer (string username,string password) {
// Debug.Log ("LoginToServer "+username+" "+password);
yield return new WaitForSeconds(0.1f);
loadingCanvas.SetActive (true);
yield return new WaitForSeconds(0.2f);
Uri uri = new Uri (Global.loginUrl);
Debug.Log ("ConnectToServer "+username+" "+password);
HttpWebRequest requestGET = (HttpWebRequest)WebRequest.Create (uri);
string xxx ="Basic "+ Convert.ToBase64String(Encoding.UTF8.GetBytes(username+":"+password));
requestGET.Headers.Add ("authorization", xxx);
requestGET.Method = "GET";
Debug.Log ("Before Response");
bool isExceptionCame = false;
yield return new WaitForSeconds(1f);
try{
var response = (HttpWebResponse)requestGET.GetResponse ();
Debug.Log ("After Response");
StartCoroutine( WaitForResponse(response) );
}catch(Exception e){
Debug.Log ("Exception "+e.Message);
if (e.Message.Contains ("UNAUTHORIZED") || e.Message.Contains ("Unauthorized")) {
ShowStatus (loginStatus [3]);
} else {
ShowStatus (loginStatus [4]);
}
isExceptionCame = true;
}
if (isExceptionCame) {
yield return new WaitForSeconds (0.5f);
loadingCanvas.SetActive (false);
yield break;
} else {
yield return new WaitForSeconds (1f);
//MyJsonParser ();
}
}
Is working for me
Your answer
![](https://koobas.hobune.stream/wayback/20220612144505im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to stay connected on a website to send WWWForm? 1 Answer
Not possible to cache assetbundles behind server authentication? 0 Answers
Google Play Services & Google Mobile Ads Together Issue 0 Answers
WWW HTTP Post request only works on Android if i'm comnected to Wifi, not on cellular data 1 Answer