public void setRequestMethod(String method) throws ProtocolException {...}明显参数是String类型,所以A错。
URL url = new URL(yourURL); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET");// 设置请求的方式 //urlConnection.setRequestMethod("POST");// 设置为POST我们再看看HttpURLConnection的源码是如何定义setRequestMethod()的参数的:
/** * The HTTP method (GET,POST,PUT,etc.). */ protected String method = "GET";可以看到我们只需要传入String类型的"GET"、“POST”等等。所以setRequestMethod()方法的参数一定是String类型的。
/* * @return the HTTP Status-Code, or -1 */ public int getResponseCode() throws IOException {...}根据注释,知道B正确。
* * @see java.net.URLConnection#getDoInput() * @see java.net.URLConnection#setDoInput(boolean) */ protected boolean doInput = true; ... ... public void setDoInput(boolean doinput) { if (connected) throw new IllegalStateException("Already connected"); doInput = doinput; }可知,doInput的默认值为true,所以C错。
/* * @see java.net.URLConnection#getDoOutput() * @see java.net.URLConnection#setDoOutput(boolean) */ protected boolean doOutput = false; ... ... public void setDoOutput(boolean dooutput) { if (connected) throw new IllegalStateException("Already connected"); doOutput = dooutput; }
可知,doOutput 默认值是false,所以D错。