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