Search code examples
javahttpurlconnectioninetaddresshttp-parameters

httpurlConnection with httpParams in java


I'm trying to post a file from local to another platform over a switch. When i do it with DefaultHttpClient there is no problem.

HttpParams params = new BasicHttpParams();
params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, InetAddress.getByName(interfaceIp));
DefaultHttpClient httpClientPost = new DefaultHttpClient(params);

But i have to do it with HttpURLConnection. Is there a way to do this?

for example:

httpConn = (HttpURLConnection) url.openConnection(myHttpParams);

here is my HttpUrlConn codes

            url = new URL(baseUrl+"/html/uploadimage.cgi");
            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setUseCaches(false);
            httpConn.setDoOutput(true); // indicates POST method
            httpConn.setDoInput(true);
            httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" +"----WebKitFormBoundary"+boundary);
            httpConn.setRequestProperty("Host", "192.168.1.1");
            httpConn.setRequestProperty("Connection", "keep-alive");
            httpConn.setRequestProperty("Content-Length", "16551361");
            httpConn.setRequestProperty("Cache-Control", "max-age=0");
            httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            httpConn.setRequestProperty("Origin", "http://192.168.1.1");
            httpConn.setRequestProperty("Upgrade-Insecure-Requests", "1");
            httpConn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36");
            httpConn.setRequestProperty("Referer", baseUrl + "/html/advance.html");
            httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate");
            httpConn.setRequestProperty("Accept-Language", "tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4");

            String cookie =  "Username="+ username +"; " +
                    "Password="+ cyreptedPassword +"; Language=tk; " +
                    "username="+ username +"; " +
                    "SessionID_R3="+ sessionID +"; activeMenuID=maintain_settings; activeSubmenuID=device_mngt";

            httpConn.setRequestProperty("Cookie", cookie);
            httpConn.setAllowUserInteraction(true);
            httpConn.setConnectTimeout(9999*9999999);
            outputStream = httpConn.getOutputStream();
            writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);

thanks for all.


Solution

  • HttpUrlConnection does not use http params, but it uses proxy.

    here is some information about httpUrlConnection example.