http请求

来源:互联网 发布:搓碟软件 编辑:程序博客网 时间:2024/06/15 20:12
// 请求返回结果
        String result = "";
        try
        {
            // 请求地址
            URL url = new URL(postUrl);
            URLConnection con = url.openConnection();

            con.setDoOutput(true);
            con.setRequestProperty("Pragma:", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setRequestProperty("Content-Type", "text/html");

            OutputStream os = con.getOutputStream();
            os.write(xmlInfo.getBytes("UTF-8"));
            // os.write(GZIPUtils.compressToByte(xmlInfo));//压缩xml文件
            os.flush();

            InputStream is = con.getInputStream();

            result = IOUtils.toString(is, "UTF-8");

        }
        catch (MalformedURLException e)
        {
            logger.error("URL协议、格式或者路径错误!", e);
        }
        catch (IOException e)
        {
            logger.error("装载文件--->失败!", e);
        }
0 0