1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| /*
* silvertunnel.org Netlib - Java library to easily access anonymity networks
* Copyright (c) 2009-2012 silvertunnel.org
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/>.
*/
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import org.junit.Test;
import org.silvertunnel.netlib.adapter.url.NetlibURLStreamHandlerFactory;
import org.silvertunnel.netlib.adapter.url.URLGlobalUtil;
import org.silvertunnel.netlib.api.NetFactory;
import org.silvertunnel.netlib.api.NetLayer;
import org.silvertunnel.netlib.api.NetLayerIDs;
import org.silvertunnel.netlib.api.util.TcpipNetAddress;
import org.silvertunnel.netlib.util.HttpUtil;
/**
* This class summarizes some code fragments to demonstrate the usage of
* HTTP APIs with silvertunnel.org Netlib.
*
* This class is directly visible from some Wiki pages
* and part of the silvertunnel.org Netlib documentation. The code fragments
* are collected here instead and not in the Wiki to ensure that they are
* always valid/correct and compile against the current version of the API.
* (The author saw wrong/non compilable examples in other
* projects documentation too often - therefore we document it here)
*
* @author hapke
*/
public class NetlibHttpUsageExamplesRemoteTest {
public static void main(String[] args) {
new NetlibHttpUsageExamplesRemoteTest().simple_HTTP_GET_request_with_HttpUtil();
new NetlibHttpUsageExamplesRemoteTest().execute_HTTP_GET_with_Adapter_URL();
new NetlibHttpUsageExamplesRemoteTest().execute_HTTP_GET_with_Adapter_URL_with_global_reconfiguration_of_class_URL();
}
/**
* Example: HTTP request using the very simple HttpUtil to load URL
* <a href="http://httptest.silvertunnel.org/httptest/bigtest.jsp?id=example" target="_blank">http://httptest.silvertunnel.org/htt...jsp?id=example</a>
*
* More documentation:
* <a href="http://sourceforge.net/apps/trac/silvertunnel/wiki/NetlibHttpUtil" target="_blank">http://sourceforge.net/apps/trac/sil...NetlibHttpUtil</a>
*/
@Test
public void simple_HTTP_GET_request_with_HttpUtil() {
try {
// classic: TcpipNetLayer with NetLayerIDs.TCPIP (--> HTTP over plain TCP/IP)
// anonymous: TorNetLayer with NetLayerIDs.TOR (--> HTTP over TCP/IP over Tor network)
//NetLayer lowerNetLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TOR);
NetLayer lowerNetLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TCPIP);
// wait until TOR is ready (optional) - this is only relevant for anonymous communication:
lowerNetLayer.waitUntilReady();
// prepare parameters (http default port: 80)
TcpipNetAddress httpServerNetAddress = new TcpipNetAddress("httptest.silvertunnel.org", 80);
String pathOnHttpServer = "/httptest/bigtest.jsp?id=example";
long timeoutInMs = 5000;
// do the HTTP request and wait for the response
byte[] responseBody = HttpUtil.getInstance().get(lowerNetLayer, httpServerNetAddress, pathOnHttpServer, timeoutInMs);
// print out the response
System.out.println(new String(responseBody));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Example: HTTP request using Adapter URL to load
* <a href="http://httptest.silvertunnel.org/httptest/bigtest.jsp?id=example2" target="_blank">http://httptest.silvertunnel.org/htt...sp?id=example2</a>
*
* This example does not change the global settings of java.net.URL
* and is preferred over the next example
* (execute_HTTP_GET_with_Adapter_URL_with_global_reconfiguration_of_class_URL).
*
* More documentation:
* <a href="http://sourceforge.net/apps/trac/silvertunnel/wiki/NetlibHttpUtil" target="_blank">http://sourceforge.net/apps/trac/sil...NetlibHttpUtil</a>
*/
@Test
public void execute_HTTP_GET_with_Adapter_URL() {
try {
// classic: TcpipNetLayer with NetLayerIDs.TCPIP (--> HTTP over plain TCP/IP)
// anonymous: TorNetLayer with NetLayerIDs.TOR (--> HTTP over TCP/IP over Tor network)
//NetLayer lowerNetLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TCPIP);
NetLayer lowerNetLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TCPIP);
// wait until TOR is ready (optional) - this is only relevant for anonymous communication:
lowerNetLayer.waitUntilReady();
// prepare URL handling on top of the lowerNetLayer
NetlibURLStreamHandlerFactory factory = new NetlibURLStreamHandlerFactory(false);
// the following method could be called multiple times
// to change layer used by the factory over the time:
factory.setNetLayerForHttpHttpsFtp(lowerNetLayer);
// create the suitable URL object
String urlStr = "http://httptest.silvertunnel.org/httptest/bigtest.jsp?id=example2";
URLStreamHandler handler = factory.createURLStreamHandler("http");
URL context = null;
URL url = new URL(context, urlStr, handler);
///////////////////////////////////////////////
// the rest of this method is as for every java.net.URL object,
// read JDK docs to find out alternative ways:
///////////////////////////////////////////////
// send request without POSTing data
URLConnection urlConnection = url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(false);
urlConnection.connect();
// receive and print the response
BufferedReader response = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line=response.readLine())!=null) {
System.out.println(line);
}
response.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("************************************");
System.out.println("************************************");
System.out.println("************************************");
System.out.println("************************************");
System.out.println("************************************");
System.out.println("************************************");
System.out.println("************************************");
System.out.println("************************************");
System.out.println("************************************");
System.out.println("************************************");
}
/**
* Example: HTTP request using Adapter URL to load
* <a href="http://httptest.silvertunnel.org/httptest/bigtest.jsp?id=example3" target="_blank">http://httptest.silvertunnel.org/htt...sp?id=example3</a>
*
* This example changes the global settings of java.net.URL
* and can have side effects to code in the same JVM. The way shown here
* should only be used if necessary. Otherwise, the previous example
* (execute_HTTP_GET_with_Adapter_URL_with_global_reconfiguration_of_class_URL)
* if preferred.
*
* More documentation:
* <a href="http://sourceforge.net/apps/trac/silvertunnel/wiki/NetlibHttpUtil" target="_blank">http://sourceforge.net/apps/trac/sil...NetlibHttpUtil</a>
*/
// to to influence other tests in the same JVM: do not execute @Test
public void execute_HTTP_GET_with_Adapter_URL_with_global_reconfiguration_of_class_URL() {
try {
// classic: TcpipNetLayer with NetLayerIDs.TCPIP (--> HTTP over plain TCP/IP)
// anonymous: TorNetLayer with NetLayerIDs.TOR (--> HTTP over TCP/IP over Tor network)
//NetLayer lowerNetLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TOR);
NetLayer lowerNetLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TCPIP);
// wait until TOR is ready (optional) - this is only relevant for anonymous communication:
lowerNetLayer.waitUntilReady();
// redirect URL handling (JVM global)
URLGlobalUtil.initURLStreamHandlerFactory();
// the following method could be called multiple times
// to change layer used by the global factory over the time:
URLGlobalUtil.setNetLayerUsedByURLStreamHandlerFactory(lowerNetLayer);
// create a URL object
String urlStr = "http://httptest.silvertunnel.org/httptest/bigtest.jsp?id=example3";
URL url = new URL(urlStr);
///////////////////////////////////////////////
// the rest of this method is as for every java.net.URL object,
// read JDK docs to find out alternative ways:
///////////////////////////////////////////////
// send request without POSTing data
URLConnection urlConnection = url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(false);
urlConnection.connect();
// receive and print the response
BufferedReader response = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line=response.readLine())!=null) {
System.out.println(line);
}
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} |
Partager