3

我在 Eclipse 中运行我的小程序没有问题,但是如果我在浏览器中签名并运行它,就会发生这种情况

10-abr-2013 19:54:37 org.apache.http.impl.client.DefaultHttpClient tryConnect
INFO: I/O exception (java.net.SocketException) caught when connecting to the target host: Unknown proxy type : HTTP
10-abr-2013 19:54:37 org.apache.http.impl.client.DefaultHttpClient tryConnect
INFO: Retrying connect
…
java.net.SocketException: Unknown proxy type : HTTP
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source) 

我在这里尝试使用 org.apache.http.client.HttpClient 上传一些文件

public static String executeMultiPartRequest(String urlString, File file,
        String fileName, String fileDescription) {

    System.out.println("SET URI " + urlString);
    HttpPost postRequest = new HttpPost(urlString);
    try {

        MultipartEntity multiPartEntity = new MultipartEntity();

        // The usual form parameters can be added this way
        multiPartEntity.addPart("fileDescription", new StringBody(
                fileDescription != null ? fileDescription : ""));
        multiPartEntity.addPart("fileName", new StringBody(
                fileName != null ? fileName : file.getName()));

        /*
         * Need to construct a FileBody with the file that needs to be
         * attached and specify the mime type of the file. Add the fileBody
         * to the request as an another part. This part will be considered
         * as file part and the rest of them as usual form-data parts
         */
        FileBody fileBody = new FileBody(file, "application/octect-stream");
        multiPartEntity.addPart("attachment", fileBody);

        // multiPartEntity.addPart("path", Charset.forName("UTF-8"));

        postRequest.setEntity(multiPartEntity);
    } catch (UnsupportedEncodingException ex) {
        ex.printStackTrace();
    }

    return executeRequest(postRequest);
}

private static String executeRequest(HttpRequestBase requestBase) {
    String responseString = "";

    InputStream responseStream = null;
    HttpClient client = new DefaultHttpClient();
    try {
        System.out.println("LISTO PARA ENVIAR A" + requestBase.getURI());
        HttpResponse response = client.execute(requestBase);
        if (response != null) {
            HttpEntity responseEntity = response.getEntity();

            if (responseEntity != null) {
                responseStream = responseEntity.getContent();
                if (responseStream != null) {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(responseStream));
                    String responseLine = br.readLine();
                    String tempResponseString = "";
                    while (responseLine != null) {
                        tempResponseString = tempResponseString
                                + responseLine
                                + System.getProperty("line.separator");
                        responseLine = br.readLine();
                    }
                    br.close();
                    if (tempResponseString.length() > 0) {
                        responseString = tempResponseString;
                    }
                }
            }
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (responseStream != null) {
            try {
                responseStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    client.getConnectionManager().shutdown();

    return responseString;
}

它有什么问题?

小程序使用java 1.6,httpclient-4.1.3.jar 签名和编译

4

1 回答 1

5

对于那些有这个问题的人,解决方案在这里http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e571获取 JRE 代理。

DefaultHttpClient client = new DefaultHttpClient () ;
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
                client.getConnectionManager().getSchemeRegistry(), 
                ProxySelector.getDefault());  
client.setRoutePlanner(routePlanner);
HttpResponse response = client.execute(requestBase) ;

然后我签署了所有图书馆 httpcore-4.2.3.jarhttpmime-4.2.3.jar并且httpclient-4.2.3.jar

于 2013-04-12T00:33:18.103 回答