java post with gzip
public static void main(String[] args){

    String foo = "[{"0":"12234107","1":"2012-01-27 12:59:36.227","2":"OXP","3":"CUCH-93717","4":"1","5":"CERRADA","6":"OXPD145","7":"2012-01-27 13:01:09.467","8":"UNACK_ALM"}]";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = null;
    HttpClient httpclient = null;
    HttpResponse response;
    System.out.println(foo.length());
        try {

                 httpclient = new DefaultHttpClient();
                 HttpPost httppost= new HttpPost ("http://127.0.0.1/lab/proyectos/php/java_envio_mensajes/post_json_gzip.php");
                 byte[] bgzip =  gzip(foo);
                 InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bgzip), bgzip.length);
                 httpentity.setChunked(true);
                 httppost.setEntity(httpentity);
                 response=httpclient.execute(httppost);

                 httppost.setEntity(httpentity);

                ...
                }
}


public static byte[] gzip(String foo){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = null;

    try {
        gzos = new GZIPOutputStream(baos);
        gzos.write(foo.getBytes("UTF-8"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (gzos != null) try { gzos.close(); } catch (IOException ignore) {};
    }

    return baos.toByteArray();
}

转自:http://pastebin.com/bTTCHgRk
lunzi   2012-06-06 12:26:15 评论:0   阅读:1128   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog