1

I'm trying to build a application in Unity that connects to a server to retrieve protected resources. I have some .net code which works perfect in a standard windows applications but the same code in unity always gives the following error: The remote server returned an error: (400) Bad Request. This suggests to me that Unity is doing something to my HTTP requests which my server does not like.

I'm using the .net WebClient class and my first thought of course was the difference in .net versioning. However, when I compiled my windows applications in .net 2.0 it still worked perfectly.

I can retrieve unprotected resources perfectly well in Unity. The problem is always after authentication when I try to get protected resources. The authentication seems to go through fine but on requesting protected resources, I get the Bad request. Maybe Unity is screwing up my authentication request? (I'm just doing basic authentication where I post off a username and password.)

I have already tried the WWW and UnityWebrequest classes but they definitely don't authenticate since the error message returned is that I need to authenticate. I'm currently looking at the packets of data using WireShark to find clues but nothing yet. I've also tried putting this into a dll. file and calling it from Unity but the same thing happens which makes me think even more than unity is doing something strange.

If anyone could suggest some things for me to try I'd very grateful! (I'm trying to access IBM's Jazz Team Server if that helps)

Here is the code I'm using The webclientextension is basically the standard .net web client extended to hold cookies.

WebClientExtension webClient = new WebClientExtension_();      

//Do a call to start the session
var Request1 = webClient.DownloadString(SessionStartURL);

//set username and password
var data = new NameValueCollection

{
   { "j_username", "MyUsername" },
   { "j_password", "MyPassword" },
};

//Post credentials to authentication URL
var Request2 = webClient.UploadValues("ServerAuthenticationURL", data);

//Request the protected resources (Works perfectly well in a standard windows application
var Request3 = webClient.DownloadString("URL_of_protected_resources");  
//THIS CAUSES THE BAD REQUEST ERROR

Here is the webclientextension class in case it holds any relevant clues.

public class WebClientExtension : WebClient 
{    
//cookie container
private CookieContainer _ckContainer = new CookieContainer();

//request server
protected override WebRequest GetWebRequest(Uri _url)
    { 
    WebRequest _request = base.GetWebRequest(_url);
        if (_request is HttpWebRequest)

        {
            (_request as HttpWebRequest).CookieContainer = _ckContainer;
            (_request as HttpWebRequest).Accept = "application/rdf+xml";
        }   
          _request.Headers.Add("OSLC-Core-Version: 2.0");
          _request.Timeout = 3600000;
          return _request;
     }    
}
IanG
  • 21
  • 3
  • Depends a lot on how they are protected, if its done with a login page, and then a session holds the variable confirming authentication .. you havent allowed for that, if it was simple http level you wouldnt need the authentication URL, you'd just send the login details with the URL to the sources – BugFinder Oct 31 '16 at 11:18
  • Normally the user would login to IBM Jazz with a login page. This can be done programmatically and is described as form based authentication. Here is an article from IBM explaining how it should be done. http://www.ibm.com/developerworks/rational/library/10/programmatic-authentication-and-certificate-handling-for-rational-team-concert-2-0/index.htm Like I said, I've followed these steps and it works fine outside of Unity but results in bad error requests when run from inside Unity. l – IanG Oct 31 '16 at 15:14
  • URL leads to broken link ... – BugFinder Oct 31 '16 at 15:49
  • Authenticate with `UnityWebRequest` ? Look [here](http://stackoverflow.com/a/39489237/3785314) – Programmer Oct 31 '16 at 18:49

0 Answers0