httpurlConnection与java中的httpParams

时间:2016-02-18 09:52:12

标签: java httpurlconnection inetaddress http-parameters

我正在尝试通过交换机将文件从本地发布到另一个平台。当我使用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);

感谢所有人。

1 个答案:

答案 0 :(得分:0)

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

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