java URL代理
来源:互联网 发布:sql为列设置默认值 编辑:程序博客网 时间:2024/04/28 04:34
private String makeHttpRequest(String requestUrl, HtmlMethod htmlMethod, HashMap<String, String> headers, String bodyText) throws Exception {
String responseStr = "";
HttpURLConnection urlConn = null;
try {
// AxisProperties.setProperty("http.proxyHost", "172.20.230.8");
// AxisProperties.setProperty("http.proxyPort", "808");
//
// AxisProperties.setProperty("https.proxyHost", "172.20.230.8");
// AxisProperties.setProperty("https.proxyPort", "808");
String useProxy = "true";
String host = "172.20.230.8";
String port = "808";
System.getProperties().put("http.proxySet", useProxy);
System.getProperties().put("http.proxyHost", host);
System.getProperties().put("http.proxyPort", port);
System.getProperties().put("https.proxySet", useProxy);
System.getProperties().put("https.proxyHost", host);
System.getProperties().put("https.proxyPort", port);
URL _url = new URL(requestUrl);
URLConnection conn = _url.openConnection();
if (requestUrl.toLowerCase().startsWith("https:")) {
urlConn = (HttpsURLConnection) conn;
} else {
urlConn = (HttpURLConnection) conn;
}
if (headers != null && headers.keySet().size() > 0) {
for (String key : headers.keySet()) {
String value = headers.get(key);
urlConn.setRequestProperty(key, value);
}
}
if (htmlMethod == HtmlMethod.GET) {
urlConn.setRequestMethod("GET");
} else if (htmlMethod == HtmlMethod.POST) {
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
} else if (htmlMethod == HtmlMethod.PUT) {
urlConn.setDoOutput(true);
urlConn.setRequestMethod("PUT");
}
//urlConn.setConnectTimeout(10000);
//urlConn.setReadTimeout(10000);
urlConn.connect();
if ((htmlMethod == HtmlMethod.POST || htmlMethod == HtmlMethod.PUT) && bodyText != null && bodyText.length()>0) {
OutputStream os = urlConn.getOutputStream();
os.write(bodyText.getBytes("UTF-8"));
os.flush();
}
InputStream dataInput = null;
if (urlConn.getResponseCode() >= 400) {
dataInput = urlConn.getErrorStream();
responseStr = IOUtils.toString(dataInput, "UTF-8");
throw new ApiException(responseStr);
} else {
dataInput = urlConn.getInputStream();
}
responseStr = IOUtils.toString(dataInput, "UTF-8");
} finally {
if (urlConn != null) {
urlConn.disconnect();
}
}
return responseStr;
}
String responseStr = "";
HttpURLConnection urlConn = null;
try {
// AxisProperties.setProperty("http.proxyHost", "172.20.230.8");
// AxisProperties.setProperty("http.proxyPort", "808");
//
// AxisProperties.setProperty("https.proxyHost", "172.20.230.8");
// AxisProperties.setProperty("https.proxyPort", "808");
String useProxy = "true";
String host = "172.20.230.8";
String port = "808";
System.getProperties().put("http.proxySet", useProxy);
System.getProperties().put("http.proxyHost", host);
System.getProperties().put("http.proxyPort", port);
System.getProperties().put("https.proxySet", useProxy);
System.getProperties().put("https.proxyHost", host);
System.getProperties().put("https.proxyPort", port);
URL _url = new URL(requestUrl);
URLConnection conn = _url.openConnection();
if (requestUrl.toLowerCase().startsWith("https:")) {
urlConn = (HttpsURLConnection) conn;
} else {
urlConn = (HttpURLConnection) conn;
}
if (headers != null && headers.keySet().size() > 0) {
for (String key : headers.keySet()) {
String value = headers.get(key);
urlConn.setRequestProperty(key, value);
}
}
if (htmlMethod == HtmlMethod.GET) {
urlConn.setRequestMethod("GET");
} else if (htmlMethod == HtmlMethod.POST) {
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
} else if (htmlMethod == HtmlMethod.PUT) {
urlConn.setDoOutput(true);
urlConn.setRequestMethod("PUT");
}
//urlConn.setConnectTimeout(10000);
//urlConn.setReadTimeout(10000);
urlConn.connect();
if ((htmlMethod == HtmlMethod.POST || htmlMethod == HtmlMethod.PUT) && bodyText != null && bodyText.length()>0) {
OutputStream os = urlConn.getOutputStream();
os.write(bodyText.getBytes("UTF-8"));
os.flush();
}
InputStream dataInput = null;
if (urlConn.getResponseCode() >= 400) {
dataInput = urlConn.getErrorStream();
responseStr = IOUtils.toString(dataInput, "UTF-8");
throw new ApiException(responseStr);
} else {
dataInput = urlConn.getInputStream();
}
responseStr = IOUtils.toString(dataInput, "UTF-8");
} finally {
if (urlConn != null) {
urlConn.disconnect();
}
}
return responseStr;
}
0 0
- java URL代理
- java设置代理访问URL
- java设置代理访问URL
- nginx代理,Java获取访问url
- Java中通过代理连接至指定的URL
- 读书笔记-java网络编程-5URL和URI-代理
- 通过代理实现Java代码访问指定URL
- 在线代理的url
- 正向代理、反向代理、Url重写
- 用户代理及 java.io.IOException: Server returned HTTP response code: 403 for URL
- 用户代理异常:java.io.IOException: Server returned HTTP response code: 403 for URL
- Java 代理,动态代理
- java代理静态代理
- Java url
- java url
- [Java] URL
- java--URL
- Java URL
- ubuntu 14.04 lts 连接一加手机开启 MTP 闪存模式 (锤子手机一样适用)
- 作业部落 Cmd Markdown 编辑阅读器
- ngrok 本机代码部署外网访问神器
- win7声音小图标不见了,如何修复
- 定义一个Father类和Child类,并进行测试
- java URL代理
- Hibernate——Session(二)详解
- C++ 值传递、指针传递、引用传递详解
- IOS7状态栏StatusBar官方标准适配方法
- 锅炉性能计算软件的需求
- SubClasing用法--子类重写父类方法,父类直接调用实现方式
- C 、C++区别和细节
- public protect private作用域的大小
- 设计模式(十四)迭代器模式(Iterator)-行为型