Show NetUtil.java syntax highlighted
package de.peerwriter.util;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
public class NetUtil {
public static int READ_TIME_OUT = 60000;
public synchronized static void sendObjectViaHTTPRequest(URL url, Object object) throws Exception {
HttpURLConnection init = (HttpURLConnection) url.openConnection();
init.setConnectTimeout(READ_TIME_OUT);
init.setReadTimeout(READ_TIME_OUT);
init.setDoOutput(true);
init.setUseCaches(false);
init.setRequestProperty("Content-type", "application/octet-stream");
ObjectOutputStream out = new ObjectOutputStream(init.getOutputStream());
out.writeObject(object);
out.flush();
init.getResponseCode();
}
// public synchronized static boolean testWookiSiteViaHTTPRequest(URL url) throws Exception {
// URL urlNeighbor = new URL(url + "?url=" + Wooki.getInstance().getWootSite().getId());
// HttpURLConnection init = (HttpURLConnection) urlNeighbor.openConnection();
// init.setConnectTimeout(READ_TIME_OUT);
// init.setReadTimeout(READ_TIME_OUT);
// init.setUseCaches(false);
// init.setRequestMethod("GET");
// if (init.getHeaderField("WOOKI_SERVICE") != null) {
// return true;
// }
// return false;
// }
public static URL normalize(URL url) throws Exception {
URI uri = url.toURI();
uri = uri.normalize();
String path = uri.getPath();
if (!path.startsWith("/")) {
path = "/" + path;
}
String urlStr = uri.getScheme() + "://" + uri.getHost();
int port = uri.getPort();
if (port != -1) {
urlStr = urlStr + ":" + port;
}
urlStr = urlStr + path;
return new URL(urlStr);
}
}
See more files for this project here