0

我正在开发一个上传器类,它将使用系统的代理设置来上传文件。在 IE 中,如果我启用代理设置,则不会发生上传。它停留在以下行

 System.out.println("Now uploading your file into bayfiles.com");
            HttpResponse response = httpclient.execute(httppost);

如果我禁用代理设置,则文件已成功上传。我不知道帽子是什么原因。谁能解释我为什么会这样?

然后又是一个疑问。如果我禁用代理设置,虽然上传工作正常,但无法登录到该站点。代码有什么问题吗?如果我错了,请纠正我,

谢谢。

我的代码如下,

public class BayFilesUploaderPlugin {

    private static URL u;
    private static HttpURLConnection uc;
    private static BufferedReader br;
    private static String tmp;
    private static String postURL;
    private static File file;
    private static String uploadresponse;
    private static String downloadlink;
    private static String deletelink;
    private static String sessioncookie;
    private static boolean login = false;
    private static String proxy_address;
    private static String proxy_host = "myproxy.mydomain.com";
    private static String proxy_port = "8080";

    public static void main(String[] args) throws Exception {

//        java.net.ProxySelector.setDefault(new MyProxySelector(ProxySelector.getDefault()));
        System.setProperty("java.net.useSystemProxies", "true");
//        Proxy next = ProxySelector.getDefault().select(new URI("http://www.google.com/")).iterator().next();
//
//        if (next.address() != null) {
//            proxy_address = next.address().toString();
//            proxy_host = proxy_address.substring(0, proxy_address.indexOf(":"));
//            proxy_port = proxy_address.substring(proxy_address.indexOf(":") + 1);
//
//            System.out.println("Host : " + proxy_host);
//            System.out.println("Port : " + proxy_port);
//            System.setProperty("https.proxyHost", proxy_host);
//            System.setProperty("http.proxyHost", proxy_host);
//            System.setProperty("https.proxyPort", proxy_port);
//            System.setProperty("http.proxyPort", proxy_port);
//        }




        // 

        System.setProperty("https.proxyHost", proxy_host);
        System.setProperty("http.proxyHost", proxy_host);
        System.setProperty("https.proxyPort", proxy_port);
        System.setProperty("http.proxyPort", proxy_port);

        initialize();
//        loginBayFiles();
//        System.exit(0);
        fileUpload();


    }

    private static void initialize() throws Exception {
        System.out.println("Getting upload url from bayfiles.com");
        System.setProperty("java.net.useSystemProxies", "true");
        u = new URL("http://bayfiles.com/ajax_upload?_=" + new Date().getTime());
        uc = (HttpURLConnection) u.openConnection();
        if (login) {
            uc.setRequestProperty("Cookie", sessioncookie);
        }
        br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        String k = "";
        while ((tmp = br.readLine()) != null) {
            k += tmp;
        }
        postURL = parseResponse(k, "\"upload_url\":\"", "\"");
        postURL = postURL.replaceAll("\\\\", "");
        System.out.println("Post URL : " + postURL);
    }

    public static String parseResponse(String response, String stringStart, String stringEnd) {

        response = response.substring(response.indexOf(stringStart));
        response = response.replace(stringStart, "");
        response = response.substring(0, response.indexOf(stringEnd));
        return response;
    }

    private static void fileUpload() throws Exception {

        DefaultHttpClient httpclient = new DefaultHttpClient();

//        httpclient.getCredentialsProvider().setCredentials(
//                new AuthScope("myproxy.mydomain.com", 80),
//                new UsernamePasswordCredentials("", ""));

        //    HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
        HttpHost proxy = new HttpHost("myproxy.mydomain.com", 80);

        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);


        HttpPost httppost = new HttpPost(postURL);
        file = new File("C:\\Dinesh\\naruto.txt");
        System.out.println(file.getName());
        MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        ContentBody cbFile = new FileBody(file);
        mpEntity.addPart("file", cbFile);
        httppost.setEntity(mpEntity);
        System.out.println("executing request " + httppost.getRequestLine());
        System.out.println("Now uploading your file into bayfiles.com");
        HttpResponse response = httpclient.execute(httppost);
        System.out.println("after exe");
        HttpEntity resEntity = response.getEntity();


        System.out.println(response.getStatusLine());
        if (resEntity != null) {
            uploadresponse = EntityUtils.toString(resEntity);
        }
//  
        System.out.println("Upload response : " + uploadresponse);
        downloadlink = parseResponse(uploadresponse, "\"downloadUrl\":\"", "\"");
        downloadlink = downloadlink.replaceAll("\\\\", "");
        deletelink = parseResponse(uploadresponse, "\"deleteUrl\":\"", "\"");
        deletelink = deletelink.replaceAll("\\\\", "");
        System.out.println("Download link : " + downloadlink);
        System.out.println("Delete link : " + deletelink);
    }

    public static void loginBayFiles() throws Exception {
        HttpParams params = new BasicHttpParams();
        params.setParameter(
                "http.useragent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6");
        DefaultHttpClient httpclient = new DefaultHttpClient(params);

        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(proxy_host, Integer.valueOf(proxy_port)),
                new UsernamePasswordCredentials("", ""));

        //    HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
        HttpHost proxy = new HttpHost(proxy_port, Integer.valueOf(proxy_port));

        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        System.out.println("Trying to log in to bayfiles.com");
        HttpPost httppost = new HttpPost("http://bayfiles.com/ajax_login");
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("action", "login"));
        formparams.add(new BasicNameValuePair("username", "myusername"));
        formparams.add(new BasicNameValuePair("password", "mypwd"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
        httppost.setEntity(entity);
        HttpResponse httpresponse = httpclient.execute(httppost);
        System.out.println("Getting cookies........");
        Iterator<Cookie> it = httpclient.getCookieStore().getCookies().iterator();
        Cookie escookie = null;
        while (it.hasNext()) {
            escookie = it.next();
            if (escookie.getName().equalsIgnoreCase("SESSID")) {
                sessioncookie = "SESSID=" + escookie.getValue();
                System.out.println(sessioncookie);
                login = true;
                System.out.println("BayFiles.com Login success :)");
            }
        }
        if (!login) {
            System.out.println("BayFiles.com Login failed :(");
        }
    }
}
4

1 回答 1

1

我使用默认代理选择器解决了类似的问题:

DefaultHttpClient httpclient = new DefaultHttpClient();

ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
     httpclient.getConnectionManager().getSchemeRegistry(),
     ProxySelector.getDefault());

httpclient.setRoutePlanner(routePlanner);
于 2012-06-18T06:12:57.940 回答