0

I am working on a uploader class which will use the system's proxy settings to upload the files. In IE, if i enable the proxy settings, upload is not happening. Its stucked at the following line

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

If i disable the proxy settings, then file is successfully uploaded. I dont know hat is the reason. Can anybody explain me why this happening?

Then one more doubt. Though upload is working if i disable the proxy settings, login to the site is not working. Is there anything wrong in the code? Correct me if I am wrong,

Thanks.

My code is as follows,

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 :(");
        }
    }
}

1 Answer 1

1

I solve a similar problem using the default proxy selector:

DefaultHttpClient httpclient = new DefaultHttpClient();

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

httpclient.setRoutePlanner(routePlanner);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.