I am very new to Java. I use the following code for the calling REST API, which works fine in a simple environment, but when I used it with a proxy environment. He threw a NullPointerException . I found the result in google that we need to set the proxy setting for it. I installed the proxy server according to this article http://www.javaworld.com/javaworld/javatips/jw-javatip42.html , but this does not work + base64Encode (password) , creating a syntax error.
URL url = new URL("http://examplerestapi/get/user"); URLConnection yc = url.openConnection(); in = new BufferedReader(new InputStreamReader(yc.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { sb.append(inputLine); } String res = sb.toString();
please help me set the Host proxy, port, username and password.
, NullPointerException , yc.getInputStream() null. , , .
NullPointerException
yc.getInputStream()
null
-, Proxy , :
Proxy
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("my.proxy.example.com", 3128)); URLConnection yc = url.openConnection(proxy);
, , ( , ).
. .