java 中带有 httpParams 的 httpurlConnection

httpurlConnection with httpParams in java

我正在尝试 post 通过交换机将文件从本地传输到另一个平台。当我使用 DefaultHttpClient 时没有问题。

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

但我必须使用 HttpURLConnection 来完成。有办法吗?

例如:

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

这是我的 HttpUrlConn 代码

            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);

谢谢大家。

HttpUrlConnection 不使用 http 参数,但它使用代理。

here 是关于 httpUrlConnection 示例的一些信息。