代码之家  ›  专栏  ›  技术社区  ›  Austin Hanson

使用httpclient、jame的mime4j和httppost方法上传速度较慢

  •  0
  • Austin Hanson  · 技术社区  · 15 年前

    我正在使用httppost上载一个多部分数据块,并将其输入httpclient对象执行方法,如下所示:

    HttpPost loginPost = new HttpPost(LOGIN_URL);
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("_email", mEmailAddress));
    params.add(new BasicNameValuePair("lpassword", mPassword));
    
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    
    loginPost.setEntity(entity);
    HttpResponse resp = mHttpClient.execute(loginPost);
    
    HttpPost post = new HttpPost(UPLOAD_URL);
    FileBody bin = new FileBody(file);
    MultipartEntity me = new MultipartEntity();
    
    me.addPart("stuff", new StringBody(stuff));
    me.addPart("file", bin);
    
    post.setEntity(new RequestEntityEx(me, handler));
    mHttpClient.execute(post);
    

    现在,登录和发布工作-很好,但上传速度非常慢。我已经测试了我的互联网连接,它比应该的慢得多(大约是1兆字节/秒,上传3兆字节的文件需要5分钟(而不是30秒)。

    有人有什么想法吗?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Alex Givant    14 年前

    我发现httpclient比普通的https方式慢9倍。我不知道为什么,谁都知道怎么了。

    这基本上是我的代码

    private static HttpClient httpClient = new DefaultHttpClient();
    private static HttpPost httpPost = new HttpPost(RRD_URL);
    
        public static String sendData(List<NameValuePair> data) {
        StringBuffer buffer = new StringBuffer();
        BufferedReader rd = null;
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(data));
            HttpResponse httpResponse = httpClient.execute(httpPost);
    
            rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            String line;
            while ((line = rd.readLine()) != null) {
                buffer.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace(System.out);
        } finally {
            try {
                if (rd != null) {
                    rd.close();
                }
            } catch (IOException e) {
                e.printStackTrace(System.out);
            }
        }
    
        return buffer.toString();
    }