Use HTTP get, post connected

Used in the project http get and post connected, transmission data;

public  static String doGet (String the HTTPUrl) { 
        HttpURLConnection conn = null ; 
        InputStream IS = null ; 
        BufferedReader br = null ; 
        String the Result = null ;
         the try {
             // Create a remote url link to 
            the URL of url = new new (the HTTPUrl) the URL of;
             // by remote URL connection object to open a connection 
            Conn = (the HttpURLConnection) url.openConnection ();
             // set the connection mode to GET 
            conn.setRequestMethod ( "the GET");
             // Set the timeout host servers 
            conn.setConnectTimeout (15000 );
             // Set the time to read the data returned by the remote 
            conn.setReadTimeout (60000 );
             // send a request 
            conn.connect ();
             // through the connection, Get input stream 
            IF (conn.getResponseCode () == 200 is ) { 
                IS = conn.getInputStream ();
                 // package input stream is, and to specify the character set 
                br = new new the BufferedReader ( new new the InputStreamReader (is, "UTF-. 8" ) );
                 // store data
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while((temp = br.readLine()) != null) {
                    sbf.append(temp);
                }
                result = sbf.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            close(conn,is,null,br);
        }
        return result;
    }
    
    public staticDoPost String (the HTTPUrl String, String param) { 
        HttpURLConnection conn = null ; 
        InputStream IS = null ; 
        OutputStream os = null ; 
        BufferedReader br = null ; 
        String the Result = null ;
         the try {
             // Create a remote url link to 
            the URL of url = new new (the URL of the HTTPUrl );
             // open a connection object via remote URL 
            Conn = (the HttpURLConnection) url.openConnection ();
             // set the connection mode to get
            conn.setRequestMethod ( "the POST" );
             // Set the timeout host servers 
            conn.setConnectTimeout (15000 );
             // set the time to read the data returned by the remote 
            conn.setReadTimeout (60000 ); 
            
            // defaults to false, the transmission to a remote server / write data 
            conn.setDoInput ( to true );
             // default setting is true, the current time to a remote server, set to true, this parameter is optional 
            conn.setDoOutput ( to true );
             // set the incoming parameters format: request parameter should be name1 = value1, name2 = value2 form 
            conn.setRequestProperty ( "the Content-the Type", "file application / X-WWW-form-urlencoded" );
             // Get the object by connecting an output stream
            = OS conn.getOutputStream ();
             // the output stream object to write data out 
            os.write (param.getBytes ( "UTF-. 8" ));
             // through the connection, takes an input stream 
            IF (conn.getResponseCode () = 200 is = ) { 
                IS = conn.getInputStream ();
                 // package input stream is, and to specify the character set 
                br = new new the BufferedReader ( new new the InputStreamReader (is, "UTF-. 8" ));
                 // store data 
                the StringBuffer SBF = new new the StringBuffer (); 
                String TEMP = null;
                while((temp = br.readLine()) != null) {
                    sbf.append(temp);
                }
                result = sbf.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            close(conn,is,null,br);
        }
        return result;
    }

 

Guess you like

Origin www.cnblogs.com/pluto-charon/p/11221283.html