Unity 3D couldn't connect to host. localhost with ip - unity3d

I am trying to HTTP POST to Laravel 5 using Unity.
The code is:
public void postToDB()
{
Debug.Log("in1");
WWWForm form = new WWWForm();
form.AddField("fb_id", "123123123");
WWW www = new WWW("http://localhost:777/new_fb_user",form);
StartCoroutine(WaitForRequest(www),form);
}
private IEnumerator WaitForRequest(WWW www)
{
Debug.Log("in2");
yield return www;
// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.text);
}
else
{
Debug.Log("WWW Error: " + www.error);
}
}
When I try to use the postToDB method, I get this error:
WWW Error: couldn't connect to host UnityEngine.Debug:Log(Object)
<WaitForRequest>c__Iterator0:MoveNext() (at Assets/GameManger.cs:41)
I am using Wamp and I added a crossdomain.xml file.
If I change http://localhost:777/new_fb_user to any domain like Facebook or Google, it connects to that host. The POST command works: I tested it using Postman. So, I think the error is coming from Unity.

I finally fixed it , the problem that i was running the laravel project internally
using the normal
php artisan serve
which was as creating private internal server
i fix it using Xampp
-coping the laravel project to htdocs folder in Xampp directory
-running Xampp serve
it worked perfectly
Thanks for the help every one

Related

Why does Ktor server respond with 404 if a static directory's name starts with a dot?

I'm trying to serve static files with the following guide: https://ktor.io/docs/serving-static-content.html#serve-all-resources
I put ".well-known" directory inside "resources" directory and I have test1.html with the following path: resources/.well-known/test1.html.
Also, there is the following code:
embeddedServer(Natty, port = 8080) {
routing {
static("/") {
staticBasePackage = ".well-known"
resources(".")
}
}
}
When I start a server and make a request localhost:8080/test1.html I receive 404 Not found but if I rename ".well-known" to "well-known" (without a leading dot) I receive "200" and content of test1.html.
Why does Ktor server return 404 if a directory starts with a dot?
Ktor version is 2.0.2.

hololens 2 tcp client - access permissions

I develop for hololens 2 in unity and now got a problem with socket communication.
I am trying to get TCP client on hololens 2 and if I want to connect to TCP server I got the following error on my glasses (in unity it works):
System.Exception: An attempt was made to access a socket in a way forbidden by its access permissions.
I already checked the permissions in Player Settings: InternetClient, InternetClientServer, PrivateNetworkClientServer
Using:
Unity 2019.4.2f1
Api Compatibility Level .NET 4.x
Scripting Backend: IL2CPP
Target SDK Version: 10.0.18362.0
using: Windows.Networking.Sockets.StreamSocket
any suggests?
I faced the connection issue a long time ago while working on Hololens1. I haven't worked on Hololens2 network protocols. But one of the biggest problem I faced was Hololens UWP C# and Unity C# are different from each other and they create a conflict. You may need to specify the directive for the communication. Check out this link. This guys explained the problem and solution really well.
My suggestion is if you are not sending constant requests and periodically sending the data, try creating a server based application which accepts REST API calls. That will be a much robust solution.
I am using Unity 2019.4.13f1 and tcp connection works fine.
Example code:
public string ServerIp = "192.168.31.12";
private const int PORT = 9999;
private Socket Client;
public void CreateClient()
{
Debug.Log("\r\nCreating Client...");
Thread createClientAndConnect = new Thread(() =>
{
try
{
IPEndPoint serverEP = new IPEndPoint(IPAddress.Parse(ServerIp), PORT);
Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Client.Connect(serverEP);
Thread listenToServer = new Thread(Receive);
listenToServer.IsBackground = true;
listenToServer.Start(Client);
}
catch (Exception e)
{
Debug.LogError("Error when creating client");
Debug.LogError(e.Message);
}
});
createClientAndConnect.IsBackground = true;
createClientAndConnect.Start();
Debug.Log("Client is created");
}

Unity WebClient SSL Timeout

I'm working on a Unity project that is reliyng on fetching data from a web API I set up on a public webserver. The server is currently set to self-signed ssl and requires the client to send certification to be able to read the data, if the client fails to send the cert the website returns with "403 forbidden".
I've tested this in the browser and postman and everything works fine.
I've also tested the exact same function in a pure visual studio project and it worked like a charm.
However, when I try this function in Unity I am met with the WebException "The request timed out
".
The way I'm currently doing it is via a WebClient, with an overrided method of WebRequest:
private void Connect()
{
ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback += ignoreCertCallback;
ServicePointManager.Expect100Continue = true;
using (var wc = new CertificateWebClient())
{
try
{
var responseBytes = wc.DownloadString(url);
Debug.Log(responseBytes);
Debug.Log(wc.ResponseHeaders);
}
catch (WebException e)
{
Debug.Log(e.ToString());
}
}
}
Override of WebRequest:
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
X509Certificate2 certificate = new X509Certificate2(#"C:\temp\ClientCert.pfx", "password");
request.ServerCertificateValidationCallback = delegate (System.Object obj, X509Certificate X509certificate, X509Chain chain, System.Net.Security.SslPolicyErrors errors)
{
return true;
};
Debug.Log(request.RequestUri);
(request as HttpWebRequest).ClientCertificates.Add(certificate);
request.Timeout = 2000;
return request;
}
Important to note is that I've tried the exact same functions inside of Unity with the "client.badssl.com" and their cert, and that also worked like a charm, returning the correct error codes when no cert is sent and everything and If I turn off client certification on my website, everything also works like charm...
From What I understand, It might be Mono that might be the problem as the certification is self-signed and not from a verified CA... But i've not been able to find a workaround... so any help would be great
What are your BG settings? I mean: is it on AWS? Express/NodeJS? WebGL? I am facing the same issue but when testing locally it works like a charm
I would suggest: to check if it is an infrastructure problem you might want to create a certificate for your domain (eg 'goodboy.mytest.io") that is certified from some free SSL providers (I can say LetsEncrypt just for testing) and launch a local server that's using that certificate, then go in your "hosts" file (depending on OS that you are currently running) and mock your localhost as "goodboy.mytest.io", so you can check if connecting to that domain everything goes fine without additional layers (usually placed between connection "bouncing" on web)
I'm following up this too

Oauth + Laravel + Vuejs + MongoDB 500 Internal Server Error

everyone. I am currently developing a web app. I started to develop on a laptop. Then I mapped the code folder on the laptop to my pc. On my laptop, everything works correctly. But on the PC i cant get authenticated. I get 500 Internal server error. I am using Oauth, Laravel, laravel passport, vuejs and mongodb. i am currently accessing the mongodb server remotely if it matters.
Here is the login method
methods: {
login(){
var data = {
client_id:'58a08f3588053a6d947dc902',
client_secret:'E1STUJipgmCO2h9gtkN2Vl1fTpgJCuiW2SrktshM',
grant_type:'password',
username:this.userid,
password:this.password
}
this.$http.post("http://localhost:8000/oauth/token", data)
.then(response => {
console.log(response)
})
/*
.then(function(response){
console.log(response)
})*/
}
}
Thank you

Authentication from NativeScript App(android/ios) to Sharepoint 2013 REST API

I'm trying to call Sharepoint 2013 REST API from an application developped with NativeScript(android/ios).
Using xhr or fetch NativeScript module I'm not able to authenticate correctly and call rest api.
Using Java I'm able to connect to the sharepoint server and call Rest API without problem Using this maven dependency: org.apache.httpcomponents httpclient 4.4.1.
public class SharePointClientAuthentication {
public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY),
new NTCredentials("username", "password", "https://hostname", "domain"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("http://hostname/_api/web/lists");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}
Any one know what's the equivalent to the java code with NativeScript or a way to get Windows authentication(NTLM) to work.
Thanks in advance.
Initially I thought this should be handled by the native HTTP library and exposed as a part of the XMLHttpRequest implementation in NativeScript, but then I discovered ntlm.js. It's a small library that takes care of the NTLM challenge-response for you.
I patched it up a bit, to get rid of the browser dependency and push some polyfills, and got it running. I put up a demo project here:
https://github.com/hdeshev/nativescript-ntlm-demo