java http请求

public static void getUploadInformation(String  path,String obj) throws IOException, JSONException {
        //建立链接
        URL url = new URL(path);
        HttpURLConnection connection ;
        StringBuffer sbuffer=null;
        try {
        //添加 请求内容
            connection= (HttpURLConnection) url.openConnection();
            //设置http链接属性
            connection.setDoOutput(true);// http正文内,所以须要设为true, 默认状况下是false;
            connection.setDoInput(true);// 设置是否从httpUrlConnection读入,默认状况下是true;
            connection.setRequestMethod("PUT"); // 能够根据须要 提交 GET、POST、DELETE、PUT等http提供的功能
            //connection.setUseCaches(false);//设置缓存,注意设置请求方法为post不能用缓存
            // connection.setInstanceFollowRedirects(true);

            connection.setRequestProperty("Host", "*******");  //设置请 求的服务器网址,域名,例如***.**.***.***
            connection.setRequestProperty("Content-Type", " application/json");//设定 请求格式 json,也能够设定xml格式的
            connection.setRequestProperty("Accept-Charset", "utf-8");  //设置编码语言
            connection.setRequestProperty("X-Auth-Token", "token");  //设置请求的token
            connection.setRequestProperty("Connection", "keep-alive");  //设置链接的状态
            connection.setRequestProperty("Transfer-Encoding", "chunked");//设置传输编码
connection.setRequestProperty("Content-Length", obj.toString().getBytes().length + ""); //设置文件请求的长度  

            connection.setReadTimeout(10000);//设置读取超时时间          
            connection.setConnectTimeout(10000);//设置链接超时时间           
            connection.connect();            
            OutputStream out = connection.getOutputStream();//向对象输出流写出数据,这些数据将存到内存缓冲区中          
            out.write(obj.toString().getBytes());            //out.write(new String("测试数据").getBytes());            //刷新对象输出流,将任何字节都写入潜在的流中       
            out.flush();     
            // 关闭流对象,此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中          
            out.close();           
            //读取响应           
            if (connection.getResponseCode()==200)            {
                // 从服务器得到一个输入流
InputStreamReader inputStream =new InputStreamReader(connection.getInputStream());//调用HttpURLConnection链接对象的getInputStream()函数, 将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
BufferedReader reader = new BufferedReader(inputStream);  

        String lines;                
sbuffer= new StringBuffer("");  

          while ((lines = reader.readLine()) != null) {                
            lines = new String(lines.getBytes(), "utf-8");                    
            sbuffer.append(lines);                }                
            reader.close();         
        }else{          
                Log.i(TAG,"请求失败"+connection.getResponseCode());    
            }    
        //断开链接           
         connection.disconnect();    
     } catch (IOException e) {  
              e.printStackTrace();     
     }   
 }
json数据
public  static String QueryLoginBody(String type,String userid,String checksum){
    String json="{\"type\":\""+type+"\","+"\"jid\":\""+userid+"\","+"\"checkSum\":\""+checksum+"\"}";
    return json;
}
调用方法,输入要传入的参数,而后直接把json数据放进去就行了
String json=AppUtils.QueryLoginBody("login","usr","123132");
AppUtils.getUploadInformation("http://www.xxx.com", json);
相关文章
相关标签/搜索