use of java.net.Proxy in project apjp by jvansteirteghem.
the class HTTPSRequest method open.
public void open() throws HTTPSRequestException {
try {
url = new URL(APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL[i]);
Proxy proxy = Proxy.NO_PROXY;
if (url.getProtocol().equalsIgnoreCase("HTTP") == true) {
if (APJP.APJP_HTTP_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(APJP.APJP_HTTP_PROXY_SERVER_ADDRESS, APJP.APJP_HTTP_PROXY_SERVER_PORT));
}
} else {
if (url.getProtocol().equalsIgnoreCase("HTTPS") == true) {
if (APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS, APJP.APJP_HTTPS_PROXY_SERVER_PORT));
}
}
}
urlConnection = url.openConnection(proxy);
if (urlConnection instanceof HttpsURLConnection) {
((HttpsURLConnection) urlConnection).setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession sslSession) {
String value1 = APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL[i];
String[] values1 = value1.split("/", -1);
String value2 = values1[2];
String[] values2 = value2.split(":");
String value3 = values2[0];
if (value3.equalsIgnoreCase(hostname)) {
return true;
} else {
return false;
}
}
});
}
if (url.getProtocol().equalsIgnoreCase("HTTP") == true) {
if (APJP.APJP_HTTP_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false && APJP.APJP_HTTP_PROXY_SERVER_USERNAME.equalsIgnoreCase("") == false) {
urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + new String(BASE64.encode((APJP.APJP_HTTP_PROXY_SERVER_USERNAME + ":" + APJP.APJP_HTTP_PROXY_SERVER_PASSWORD).getBytes())));
}
} else {
if (url.getProtocol().equalsIgnoreCase("HTTPS") == true) {
if (APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false && APJP.APJP_HTTPS_PROXY_SERVER_USERNAME.equalsIgnoreCase("") == false) {
urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + new String(BASE64.encode((APJP.APJP_HTTPS_PROXY_SERVER_USERNAME + ":" + APJP.APJP_HTTPS_PROXY_SERVER_PASSWORD).getBytes())));
}
}
}
for (int j = 0; j < APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i].length; j = j + 1) {
if (APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i][j].equalsIgnoreCase("") == false) {
urlConnection.setRequestProperty(APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i][j], APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_VALUE[i][j]);
}
}
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
} catch (Exception e) {
throw new HTTPSRequestException("HTTPS_REQUEST/OPEN", e);
}
}
use of java.net.Proxy in project nutz by nutzam.
the class Http method setSocktProxy.
/*
* please use setSocketProxy method
*/
@Deprecated
public static void setSocktProxy(String host, int port) {
final Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(host, port));
proxySwitcher = new ProxySwitcher() {
public Proxy getProxy(URL url) {
return proxy;
}
public Proxy getProxy(Request req) {
if ("close".equals(req.getHeader().get("NoProxy")))
return null;
String url = req.getUrl().toString();
if (url.startsWith("http") && url.contains("://") && url.length() > "https://".length()) {
url = url.substring(url.indexOf("://"));
if (url.startsWith("127.0.0") || url.startsWith("localhost"))
return null;
}
req.getHeader().set("Connection", "close");
return getProxy(req.getUrl());
}
};
}
use of java.net.Proxy in project weiciyuan by qii.
the class JavaHttpUtility method doUploadFile.
public boolean doUploadFile(String urlStr, Map<String, String> param, String path, String imageParamName, final FileUploaderHttpHelper.ProgressListener listener) throws WeiboException {
String BOUNDARYSTR = getBoundry();
File targetFile = new File(path);
byte[] barry = null;
int contentLength = 0;
String sendStr = "";
try {
barry = ("--" + BOUNDARYSTR + "--\r\n").getBytes("UTF-8");
sendStr = getBoundaryMessage(BOUNDARYSTR, param, imageParamName, new File(path).getName(), "image/png");
contentLength = sendStr.getBytes("UTF-8").length + (int) targetFile.length() + 2 * barry.length;
} catch (UnsupportedEncodingException e) {
}
int totalSent = 0;
String lenstr = Integer.toString(contentLength);
HttpURLConnection urlConnection = null;
BufferedOutputStream out = null;
FileInputStream fis = null;
GlobalContext globalContext = GlobalContext.getInstance();
String errorStr = globalContext.getString(R.string.timeout);
globalContext = null;
try {
URL url = null;
url = new URL(urlStr);
Proxy proxy = getProxy();
if (proxy != null) {
urlConnection = (HttpURLConnection) url.openConnection(proxy);
} else {
urlConnection = (HttpURLConnection) url.openConnection();
}
urlConnection.setConnectTimeout(UPLOAD_CONNECT_TIMEOUT);
urlConnection.setReadTimeout(UPLOAD_READ_TIMEOUT);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Charset", "UTF-8");
urlConnection.setRequestProperty("Content-type", "multipart/form-data;boundary=" + BOUNDARYSTR);
urlConnection.setRequestProperty("Content-Length", lenstr);
((HttpURLConnection) urlConnection).setFixedLengthStreamingMode(contentLength);
urlConnection.connect();
out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(sendStr.getBytes("UTF-8"));
totalSent += sendStr.getBytes("UTF-8").length;
fis = new FileInputStream(targetFile);
int bytesRead;
int bytesAvailable;
int bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024;
bytesAvailable = fis.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fis.read(buffer, 0, bufferSize);
long transferred = 0;
final Thread thread = Thread.currentThread();
while (bytesRead > 0) {
if (thread.isInterrupted()) {
targetFile.delete();
throw new InterruptedIOException();
}
out.write(buffer, 0, bufferSize);
bytesAvailable = fis.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fis.read(buffer, 0, bufferSize);
transferred += bytesRead;
if (transferred % 50 == 0) {
out.flush();
}
if (listener != null) {
listener.transferred(transferred);
}
}
out.write(barry);
totalSent += barry.length;
out.write(barry);
totalSent += barry.length;
out.flush();
out.close();
if (listener != null) {
listener.waitServerResponse();
}
int status = urlConnection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
String error = handleError(urlConnection);
throw new WeiboException(error);
}
targetFile.delete();
} catch (IOException e) {
e.printStackTrace();
throw new WeiboException(errorStr, e);
} finally {
Utility.closeSilently(fis);
Utility.closeSilently(out);
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return true;
}
use of java.net.Proxy in project weiciyuan by qii.
the class JavaHttpUtility method doGet.
public String doGet(String urlStr, Map<String, String> param) throws WeiboException {
GlobalContext globalContext = GlobalContext.getInstance();
String errorStr = globalContext.getString(R.string.timeout);
globalContext = null;
InputStream is = null;
try {
StringBuilder urlBuilder = new StringBuilder(urlStr);
urlBuilder.append("?").append(Utility.encodeUrl(param));
URL url = new URL(urlBuilder.toString());
AppLogger.d("get request" + url);
Proxy proxy = getProxy();
HttpURLConnection urlConnection;
if (proxy != null) {
urlConnection = (HttpURLConnection) url.openConnection(proxy);
} else {
urlConnection = (HttpURLConnection) url.openConnection();
}
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.setConnectTimeout(CONNECT_TIMEOUT);
urlConnection.setReadTimeout(READ_TIMEOUT);
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Charset", "UTF-8");
urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
urlConnection.connect();
return handleResponse(urlConnection);
} catch (IOException e) {
e.printStackTrace();
throw new WeiboException(errorStr, e);
}
}
use of java.net.Proxy in project weiciyuan by qii.
the class JavaHttpUtility method getProxy.
private static Proxy getProxy() {
String proxyHost = System.getProperty("http.proxyHost");
String proxyPort = System.getProperty("http.proxyPort");
if (!TextUtils.isEmpty(proxyHost) && !TextUtils.isEmpty(proxyPort)) {
return new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.valueOf(proxyPort)));
} else {
return null;
}
}
Aggregations