0

I made a webserver in asp.net with some API. The server run with IIS in localhost, and i have to connect to it from my Android app (using the android studio emulator). The problem is that i can't connect to the webserver. I tried with ngrok and when i make a request I get

Bad Request - Invalid Hostname
HTTP Error 400. The request hostname is invalid.

I also tried Conveyor but when i make a request I get

I/System.out: Volley error: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

Can someone help me please? I have no idea how to solve this.. all the tutorials and questions say the same things and not work for me.

Here the code of kotlin request:

        // i don't put the correct url in the question for security
        val url = "https://255.255.255.255:44307/api/login"


        // Post parameters
        // Form fields and values
        val params = HashMap<String,String>()
        params["mail"] = email.text.toString()
        params["pw"] = password.text.toString()
        val jsonObject = JSONObject(params as Map<*, *>)

        // Volley post request with parameters
        val request = JsonObjectRequest(
            Request.Method.POST, url, jsonObject,
            { response ->
                // Process the json
                try {
                    println( "Response:$response")
                    val intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)
                    finish()
                    //textView.text = "Response: $response"
                } catch (e: Exception) {
                    println( "Response:$response")

                    //textView.text = "Exception: $e"
                }

            }, {
                // Error in request
                println("Volley error: $it")

                //textView.text = "Volley error: $it"
            })


        // Volley request policy, only one time request to avoid duplicate transaction
        request.retryPolicy = DefaultRetryPolicy(
            DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
            // 0 means no retry
            0, // DefaultRetryPolicy.DEFAULT_MAX_RETRIES = 2
            1f // DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
        )

        // Add the volley post request to the request queue
        VolleySingleton.getInstance(this).addToRequestQueue(request)
            // Add the volley post request to the request queue
            VolleySingleton.getInstance(this).addToRequestQueue(request)
azziza432
  • 25
  • 2
  • 5
  • I think you’ll need to provide more info. Like your android code that’s trying to connect, for example. Hard to see what you’re doing wrong with only the error messages but none of the code... – Nicholas Rees Feb 10 '21 at 08:45
  • @NicholasRees even if i try to make a request with postman using the url given by ngrok the request fail. with Conveyor the request with postman works, but if i try with android i get the second error in the question – azziza432 Feb 10 '21 at 08:52
  • @NicholasRees i put the code – azziza432 Feb 10 '21 at 08:57
  • @azziza432 Check this answer, might be helpful - https://stackoverflow.com/a/36645473/2275775 – Michał Jarzyna Feb 10 '21 at 09:00
  • 1
    You're using localhost so, it cannot be "https", use "http" and add `android:usesCleartextTraffic="true"` in your `AndroidManifest.xml` application tag and it will work. – ʍѳђઽ૯ท Feb 10 '21 at 09:01
  • The code helps. First thing I see is that you’re using https with an IP address. That can’t work. You either need to use http or get a domain name and certificate for your IIS server and update your code to call the service by name instead of IP. Calling via http will be easier but insecure, so... my suggestion would be figure out how to make it work via http for development. Then figure out how to make it work with https before you go live to real clients in the real world. – Nicholas Rees Feb 10 '21 at 09:02
  • @MichałJarzyna i tried and get the same: I/System.out: Volley error: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. – azziza432 Feb 10 '21 at 09:16
  • @NicholasRees the problem is that the IIS run the server in https:localhost:5000 for example, so i have to use https – azziza432 Feb 10 '21 at 09:18
  • 1
    i solve removing the ssl from the server and using the @MichałJarzyna tip – azziza432 Feb 10 '21 at 10:33

1 Answers1

0

I solve the problem removing the ssl from the server and using the tip of @Michal

azziza432
  • 25
  • 2
  • 5