doPost封装

private SessionInfo getSessionInfo() {
            
        String jsonResult = null;
        InputStream is = null;
            
        try {
            is = doPost(bugfreeUrl, "mode=getsid");
            jsonResult = getResult(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
            
        if (jsonResult == null) {
            return null;
        }
            
        SessionInfo sessionInfo = JsonParser.parseSessionInfo(jsonResult);
            
        System.out.println("sessionId: " + sessionInfo.getSessionId());
        System.out.println("rand: " + sessionInfo.getRand());
            
        return sessionInfo;
    }
public InputStream doPost(String urlStr, Map<String, String> params) throws IOException {
        StringBuilder sb = new StringBuilder();
          
        for(Map.Entry<String, String> param : params.entrySet()) {
            sb.append(param.getKey()).append("=").append(param.getValue()).append("&");
        }
          
        int len = sb.length();
        if (len > 0) {
            sb.deleteCharAt(len - 1);
        }
          
        String args = sb.toString();
        //System.out.println("args: " + args);
        return doPost(urlStr, args);
    }
      
    public InputStream doPost(String urlStr, String args) throws IOException {
          
        URL url = null;
        try {
            url = new URL(urlStr);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
          
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();    
        //connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
          
        //String myCookie = "GerritAccount=aSecfaQr028r8-6yxggWFA1QQc0YQFz5";
        if (sessionId != null) {
            System.out.println("sessionId: " + sessionId);
            connection.setRequestProperty("Cookie", sessionId);
        }
          
        //connection.setRequestProperty("Connection", "Keep-Alive");
          
        /**  
         * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。  
         * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做:  
         */   
        connection.setDoOutput(true);
        //connection.setRequestMethod("POST");
          
        /**  
         * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ...  
         */   
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");    
        out.write(args); //向页面传递数据。post的关键所在!    
        // remember to clean up    
        out.flush();    
        out.close();    
          
        return connection.getInputStream();
    }
      
    private static String getResult(InputStream is) throws IOException {
        StringBuilder sb = new StringBuilder();
          
        BufferedReader br = new BufferedReader(new InputStreamReader(    
                is));    
          
        String line = null;
        while ((line = br.readLine()) != null) {    
            sb.append(line);    
        }    
          
        return sb.toString();
    }


你可能感兴趣的:(post)