java 调用接口 Server returned HTTP response code: 411 for URL

//中文必须在URLEncoder后传递,编码为gb2312
		String content = java.net.URLEncoder.encode("您本次操作的短信验证码为"+code,"gb2312");
		//拼接访问短信接口的url
		String sendUrl = "http://si.800617.com:4400/SendSms.aspx?un="+uname+"&pwd="+pwd+"&mobile="+clientTel+"&msg="+content;
		System.out.println(sendUrl);
		URL url = new URL(sendUrl);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setRequestMethod("GET");
		connection.connect();
		String result = "";
		InputStreamReader  bis = new InputStreamReader(connection.getInputStream(),"gb2312");
		int c = 0;
		while((c = bis.read()) != -1){
			result = result + (char)c;   
		}
	    return result;


执行的时候,总是报Server returned HTTP response code: 411 for URL的错误,一开始是报Server returned HTTP response code: 400 for URL的错误,后来URLEncoder.encode设置编码以后就报411的错了,本以为还是中文的问题,后把connection.setRequestMethod("GET");改成GET的就可以,之前是POST。

你可能感兴趣的:(java,webservice,URLEncoder)