safsdafsdsdf

来源:互联网 发布:淘宝卖家用的什么软件 编辑:程序博客网 时间:2024/05/31 15:18
  public static String accessNetworkByPost(String urlString, String contents) throws IOException {
        DataOutputStream out = null;
        URL postUrl;
        String result = null;
//        BufferedInputStream bis = null;
//        ByteArrayBuffer baf = null;
        HttpURLConnection connection = null;
        try {
            byte[] encrypted = contents.getBytes("utf-8");
            postUrl = new URL(urlString);
            connection = (HttpURLConnection) postUrl.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(20000);
            connection.setRequestMethod("POST");
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("contentType", "utf-8");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Content-Length", "" + encrypted.length);
            
            out = new DataOutputStream(connection.getOutputStream());
            out.write(encrypted);
            out.flush();
            out.close();


//            bis = new BufferedInputStream(connection.getInputStream());
//            baf = new ByteArrayBuffer(1024);
            result=dealResponseResult(connection.getInputStream());


        } catch (Exception e) {
            e.printStackTrace();
            Log.i("error", "NetworkUtil accessNetworkByPost exception:" + e.toString());
        } finally {
            if (connection != null) connection.disconnect();
//            if (bis != null) bis.close();
//            if (baf != null) baf.clear();
        }
        return result;


    }
0 0
原创粉丝点击