gpt4 book ai didi

java - 无法使用具有代理支持的 httpclient 上传文件

转载 作者:行者123 更新时间:2023-12-01 15:19:58 25 4
gpt4 key购买 nike

我正在开发一个 uploader 类,它将使用系统的代理设置来上传文件。在 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 :(");
}
}
}

最佳答案

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

DefaultHttpClient httpclient = new DefaultHttpClient();

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

httpclient.setRoutePlanner(routePlanner);

关于java - 无法使用具有代理支持的 httpclient 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11077339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com