java.net.MalformedURLException: no protocol: Solution

Today I found an error when doing a simulated request:

java.net.MalformedURLException: no protocol: 

Methods as below:

public static InputStream getInputStreamFromUrl(String urlstr){         try {                  System.out.println("urlstr:"+urlstr);             urlstr="192.168.1.173:8080/Api/petinfo/petinfo?flag=adopt&json=[{\"pettype\":\"100\",\"petname\":\"ll\"}]";             InputStream is = null;             HttpURLConnection conn = null;             URL url = new URL(urlstr);             conn = (HttpURLConnection) url.openConnection();             if (conn.getResponseCode() == 200) {                 is = conn.getInputStream();                 return is;             }         } catch (Exception e) {             e.printStackTrace();         }














        return null;
    }

The method is correct, but in the end, the above exception was reported, which is very surprising. .

Finally, after many experiments, I finally found the reason.

Because I forced the switch here and converted URLConnection to HttpURLConnection, so the front of the url should add "http:\\192.168.1.173:8080/Api/petinfo/petinfo?flag=adopt&json=[{\"pettype\":\" 100\",\"petname\":\"ll\"}]";";

This will not go wrong, or HttpURLConnection is changed to URConnectionL

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324129873&siteId=291194637