📄 webclient.java
字号:
/*
* Created on Jul 31, 2006
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package org.GTADS.helper;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.swing.text.JTextComponent;
/**
* @author sday
*
* Class for general purpose web functions
* intially used for www.whatismyip.com web request
*
*/
public class WebClient {
public static String getWhatIsMyIP(JTextComponent statusObject){
String externalIPAddress = "";
String buffer = new String();
boolean EOF = false;
setStatusOnTextObject(statusObject, "Retrieveing IP...");
try {
URL whatIsMyIPDotCom = new URL ("http://www.whatismyip.com");
whatIsMyIPDotCom.openConnection();
InputStream webData = whatIsMyIPDotCom.openStream();
DataInputStream din = new DataInputStream(webData);
while (true){
String s = new String();
s = din.readLine();
if (s == null)
break;
else
buffer += s;
}
externalIPAddress = parseURLFromMyIPSite(buffer);
} catch (IOException ioe){
externalIPAddress = "Error getting IP Address";
}
return externalIPAddress;
}
public static String webGet(String urlRequest){
String content = new String();
StringBuffer contentStream = new StringBuffer();
try {
URL genericURLRequest = new URL (urlRequest);
genericURLRequest.openConnection();
InputStream webData = genericURLRequest.openStream();
DataInputStream din = new DataInputStream(webData);
while (true){
String s = new String();
s = din.readLine();
if (s == null)
break;
else
contentStream.append(s);
}
} catch (Exception e){
return null;
}
return new String(contentStream);
}
private static String parseURLFromMyIPSite(String webUserData){
String getIP = new String("");
String searchString1 = "<TITLE>WhatIsMyIP.com - ";
String searchString2 = "</TITLE>";
int startPos = searchString(webUserData, searchString1);
int endPos = searchString(webUserData, searchString2);
if (startPos != -1 && endPos != -1 &&
endPos > startPos){
getIP = webUserData.substring(startPos, endPos - searchString2.length());
}
return getIP;
}
private static int searchString(String text, String searchString){
int pos = -1;
int searchPos = 0;
for (int i = 0; i <= text.length(); i++){
if (searchPos >= searchString.length()){
pos = i;
break;
}
if (text.charAt(i) == searchString.charAt(searchPos)){
searchPos++;
}
else {
searchPos = 0;
}
}
return pos;
}
private static void setStatusOnTextObject(JTextComponent statusObject, String newText){
if (statusObject != null){
statusObject.setText(newText);
}
}
public static void main(String[] args) {
System.out.print(getWhatIsMyIP(null));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -