Skip to content

Commit cfcb154

Browse files
author
laizhihuan
committed
url and uri
1 parent a290a1a commit cfcb154

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

src/urlAndUri/ContentGetter.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package urlAndUri;
2+
3+
import java.io.IOException;
4+
import java.net.MalformedURLException;
5+
import java.net.URL;
6+
7+
/**
8+
* @Description: download object
9+
* @Author : lzh laizhihuan@basecity.com
10+
* @Date : 2013-3-24 下午12:12:07
11+
*
12+
*/
13+
public class ContentGetter {
14+
public static void main(String[] args) {
15+
if(args.length == 0) return;
16+
17+
try {
18+
URL u = new URL(args[0]);
19+
Object o = u.getContent();
20+
System.out.println("I got a " + o.getClass().getName());
21+
} catch (MalformedURLException e) {
22+
System.err.println(e);
23+
} catch (IOException e) {
24+
System.err.println(e);
25+
}
26+
}
27+
}

src/urlAndUri/EncoderTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package urlAndUri;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.net.URLEncoder;
5+
6+
/**
7+
* @Description: x-www-form-urlencoded的字符串
8+
* @Author : lzh laizhihuan@basecity.com
9+
* @Date : 2013-3-24 下午4:03:45
10+
*
11+
*/
12+
public class EncoderTest {
13+
public static void main(String[] args) throws UnsupportedEncodingException {
14+
System.out.println(URLEncoder.encode("This string has spaces", "UTF-8"));
15+
System.out.println(URLEncoder.encode("This*string*has*asterisks", "UTF-8"));
16+
System.out.println(URLEncoder.encode("This+string+has+pluses", "UTF-8"));
17+
System.out.println(URLEncoder.encode("This/string/has/slashes", "UTF-8"));
18+
System.out.println(URLEncoder.encode("This\"string\"has\"quote\"marks", "UTF-8"));
19+
System.out.println(URLEncoder.encode("This:string:has:colons", "UTF-8"));
20+
21+
}
22+
}

src/urlAndUri/LowPortScanner.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package urlAndUri;
2+
3+
import java.io.IOException;
4+
import java.net.Socket;
5+
import java.net.UnknownHostException;
6+
7+
/**
8+
* @Description: 查看指定主机上前1024个端口那些可以提供TCP服务
9+
* @Author : lzh laizhihuan@basecity.com
10+
* @Date : 2013-3-27 下午2:42:47
11+
*
12+
*/
13+
public class LowPortScanner {
14+
public static void main(String[] args) {
15+
String host = "127.0.0.1";
16+
for(int i=1; i<1024; i++) {
17+
try {
18+
Socket s = new Socket(host,i);
19+
System.out.println("There is a server on port " + i + " of " + host);
20+
} catch (UnknownHostException e) {
21+
System.err.println(e);
22+
} catch (IOException e) {
23+
}
24+
}
25+
}
26+
}

src/urlAndUri/SourceViewer.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package urlAndUri;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
import java.io.Reader;
8+
import java.net.MalformedURLException;
9+
import java.net.URL;
10+
11+
/**
12+
* @Description: download web page
13+
* @Author : lzh laizhihuan@basecity.com
14+
* @Date : 2013-3-24 下午12:04:28
15+
*
16+
*/
17+
public class SourceViewer {
18+
public static void main(String[] args) throws IOException {
19+
String url = "http://www.oreilly.com";
20+
try {
21+
URL u = new URL(url);
22+
InputStream in = u.openStream();
23+
24+
in = new BufferedInputStream(in);
25+
Reader r = new InputStreamReader(in);
26+
27+
int c;
28+
while((c = r.read()) != -1) {
29+
System.out.print((char) c);
30+
}
31+
32+
} catch (MalformedURLException e) {
33+
System.err.println(url + "is not a pareseable URL");
34+
35+
}
36+
37+
}
38+
}

0 commit comments

Comments
 (0)