HttpConnection

HttpConnection
import  java.io.BufferedReader;
import  java.io.DataOutputStream;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.net.HttpURLConnection;
import  java.net.MalformedURLException;
import  java.net.URL;

public   class  TestConn  {

    
public TestConn() {
        
super();
    }


    
public static void main(String[] args) throws Exception {
        String url 
= null;
        getHttpText(url);
        System.out.println(
"abc");
    }


    
public static String getHttpText(String str) {
        URL url 
= null;
        
try {
            url 
= new URL(str);
        }
 catch (MalformedURLException e) {
            e.printStackTrace();
        }

        HttpURLConnection huc 
= null;
        
try {

            huc 
= (HttpURLConnection) url.openConnection();
            huc.setRequestMethod(
"GET");
            huc.setRequestProperty(
"Content-Type",
                    
"application/vnd.syncml.dm+xml");
            
// application/x-www-form-urlencoded
            
// application/vnd.syncml.dm+xml
            huc.setDoOutput(true);
            huc.setRequestProperty(
"Cache-Control""private");
            huc.setRequestProperty(
"Accept-Charset""utf-8");
            huc.setRequestProperty(
"Accept""application/vnd.syncml.dm+xml");
            huc.setRequestProperty(
"Content-Length""3");
            DataOutputStream printout;
            printout 
= new DataOutputStream(huc.getOutputStream());
            printout.writeBytes(
"abc");
        }
 catch (IOException e) {
            e.printStackTrace();
        }

        InputStream is 
= null;
        
try {
            is 
= huc.getInputStream();
        }
 catch (IOException e) {
            e.printStackTrace();
        }

        BufferedReader br 
= new BufferedReader(new InputStreamReader(is));
        StringBuffer sb 
= new StringBuffer();
        String line 
= "";
        
try {
            
while ((line = br.readLine()) != null{
                sb.append(line).append(
"\n");
            }

        }
 catch (IOException e) {
            e.printStackTrace();
        }

        
return sb.toString();
    }


}

你可能感兴趣的:(HttpConnection)