日期:2014-05-17  浏览次数:20822 次

http接口,报空指针,请教大虾
项目中要做一个发短信的功能,客户给了一个http接口url,一开始以为直接这样用BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 后来那个开发接口的公司说不能这样使用,也不能直接在浏览器窗口中输入url的请求:因为他们后台是这么处理的:
String result = "";
request.setCharacterEncoding("GBK");
BufferedReader br = request.getReader();
String line = br.readLine();
String[] params = line.split("&");
Map<String,Object> param = new HashMap<String,Object>();
for(String s : params){
String[] keyAndValue = s.split("=");
String key = keyAndValue[0];
String value = keyAndValue[1];
if(key.equals("SMSContent") || key.equals("DestMobile")) {
value = URLDecoder.decode(value,"GBK");
}
param.put(key, value);
if(!isOpen)System.out.println(key+":"+value);
}
if(!isOpen)System.out.println("");
String BizType = param.get("BizType").toString();
if(BizType.equals("01")){
result = smsWordService.senMsgServiceAPI(param);
}else if(BizType.equals("02")){
System.out.println("已发短信查询");
}else if(BizType.equals("03")){
System.out.println("上行短信查询");
}

后来httpClient.executeMethod(getMethod);
    byte[] responseBody = getMethod.getResponseBody();
    strHtml = new String(responseBody);
还是报空指针的错误,他们说给的url是正确的,是在不知道怎么写了

------解决方案--------------------
URL url = new URL("http://127.0.0.1:8080/abc/index.jsp");
HttpURLConnection connection =  (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
//connection.setChunkedStreamingMode(0);
connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
connection.connect();
//System.out.println(connection.getResponseCode());
String params = "userName=123&pwd=456";
OutputStream os = new BufferedOutputStream(connection.getOutputStream());