本文整理汇总了Java中java.net.HttpURLConnection.setDefaultUseCaches方法的典型用法代码示例。如果您正苦于以下问题:Java HttpURLConnection.setDefaultUseCaches方法的具体用法?Java HttpURLConnection.setDefaultUseCaches怎么用?Java HttpURLConnection.setDefaultUseCaches使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.HttpURLConnection
的用法示例。
在下文中一共展示了HttpURLConnection.setDefaultUseCaches方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initConnection
import java.net.HttpURLConnection; //导入方法依赖的package包/类
/**
* Initial the connection for POST and GET method.
*
* @param connection
* @param method
* @param requestProperties
* @param connectTimeOut
* @param readTimeOut
* @throws IOException
*/
private static void initConnection(HttpURLConnection connection, String method,
Map<String, String> requestProperties, Long connectTimeOut, Long readTimeOut) throws IOException {
if (connection != null) {
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
connection.setDoInput(true);
if (method != null && method.trim().length() > 0) {
try {
connection.setRequestMethod(method);
} catch (ProtocolException e) {
throw new IOException("Set Request Method Error: " + e);
}
if (SEND_METHOD_POST.equals(method)) {
connection.setDoOutput(true);
}
} else {
throw new IOException("Error Method Parameter: " + method);
}
if (requestProperties != null && requestProperties.size() > 0) {
String key, value;
for (Map.Entry<String, String> tempEntry : requestProperties.entrySet()) {
key = tempEntry.getKey();
value = tempEntry.getValue();
connection.setRequestProperty(key, value);
}
}
if (connectTimeOut != null && readTimeOut != null) {
System.setProperty("sun.net.client.defaultConnectTimeout", connectTimeOut.toString());
System.setProperty("sun.net.client.defaultReadTimeout", readTimeOut.toString());
connection.setConnectTimeout(connectTimeOut.intValue());
connection.setReadTimeout(readTimeOut.intValue());
}
connection.setRequestProperty("User-Agent", "Mozilla/MSIE");
} else {
throw new IOException("Error HttpURLConnection Parameter connection is null.");
}
}
示例2: run
import java.net.HttpURLConnection; //导入方法依赖的package包/类
@Override
public void run()
{
super.run();
StringBuilder contents = new StringBuilder();
String tempContents;
if (this.requestMode.equals("GET") && this.params.size() > 0)
{
this.targetUrl += "?" + this.getEncodedParams();
}
try
{
URL url = new URL(this.targetUrl);
URLConnection conn = (URLConnection) url.openConnection();
HttpURLConnection hconn = (HttpURLConnection) conn;
hconn.setRequestMethod(this.requestMode);
hconn.setDoOutput(true);
hconn.setDoInput(true);
hconn.setUseCaches(true);
hconn.setDefaultUseCaches(true);
if (this.params.size() > 0 && this.requestMode.equals("POST"))
{
byte[] postParams = this.getEncodedParams().getBytes("UTF-8");
hconn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
hconn.setRequestProperty("Content-Length", String.valueOf(postParams.length));
OutputStream writer = hconn.getOutputStream();
writer.write(postParams);
writer.flush();
writer.close();
}
BufferedReader buff = new BufferedReader(new InputStreamReader (hconn.getInputStream(), "UTF-8"));
while((tempContents = buff.readLine()) != null)
{
contents.append(tempContents);
}
buff.close();
this.doneHandler.func(contents);
}
catch (Exception e)
{
e.printStackTrace();
}
}
示例3: getData
import java.net.HttpURLConnection; //导入方法依赖的package包/类
public static HttpResponse getData(RequestType type, String url, String post, HashMap<String, String> headers) {
Thread.setDefaultUncaughtExceptionHandler(com.gmt2001.UncaughtExceptionHandler.instance());
HttpResponse r = new HttpResponse();
r.type = type;
r.url = url;
r.post = post;
r.headers = headers;
try {
URL u = new URL(url);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
for (Entry<String, String> e : headers.entrySet()) {
h.addRequestProperty(e.getKey(), e.getValue());
}
h.setRequestMethod(type.name());
h.setUseCaches(false);
h.setDefaultUseCaches(false);
h.setConnectTimeout(timeout);
h.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 QuorraBotJ/2015");
if (!post.isEmpty()) {
h.setDoOutput(true);
}
h.connect();
if (!post.isEmpty()) {
BufferedOutputStream stream = new BufferedOutputStream(h.getOutputStream());
stream.write(post.getBytes());
stream.flush();
stream.close();
}
if (h.getResponseCode() < 400) {
r.content = IOUtils.toString(new BufferedInputStream(h.getInputStream()), h.getContentEncoding());
r.httpCode = h.getResponseCode();
r.success = true;
} else {
r.content = IOUtils.toString(new BufferedInputStream(h.getErrorStream()), h.getContentEncoding());
r.httpCode = h.getResponseCode();
r.success = false;
}
} catch (IOException ex) {
r.success = false;
r.httpCode = 0;
r.exception = ex.getMessage();
com.gmt2001.Console.err.printStackTrace(ex);
}
return r;
}