📄 httptest.java
字号:
if (is != null) {
try {
is.close();
} catch (Exception ce) {
;
}
}
if (c != null) {
try {
c.close();
} catch (Exception ce) {
;
}
}
setCommands(t, false);
display.setCurrent(t);
}
/**
* Read the header of an HTTP connection. Don't care about
* the actual data.
*
* All response header fields are displayed in a TextBox screen.
* @param request type of HTTP request (GET or POST)
*/
private void readHeaders(String request) {
HttpConnection c;
TextBox t;
StringBuffer b;
try {
try {
c = (HttpConnection)Connector.open(url);
} catch (IllegalArgumentException e) {
String m = e.getMessage();
t = new TextBox("Illegal argument", e.getMessage(), 128, 0);
setCommands(t, false);
display.setCurrent(t);
return;
} catch (ConnectionNotFoundException e) {
t = new TextBox("Error", "Protocol not supported", 128, 0);
setCommands(t, false);
display.setCurrent(t);
return;
} catch (Exception e) {
t = new TextBox("Error", e.toString(), 128, 0);
setCommands(t, false);
display.setCurrent(t);
return;
}
try {
c.setRequestMethod(request);
b = new StringBuffer();
b.append("URL: ");
b.append(c.getURL());
b.append("\nProtocol: ");
b.append(c.getProtocol());
b.append("\nHost: " + c.getHost());
b.append("\nFile: " + c.getFile());
b.append("\nRef: " + c.getRef());
b.append("\nQuery: ");
b.append(c.getQuery());
b.append("\nPort: ");
b.append(c.getPort());
b.append("\nMethod: ");
b.append(c.getRequestMethod());
if (c instanceof HttpsConnection) {
// getSecurityInfo should connect
SecurityInfo sslInfo = ((HttpsConnection)c).getSecurityInfo();
Certificate cert = sslInfo.getServerCertificate();
b.append("\nSecure protocol: ");
b.append(sslInfo.getProtocolName());
b.append("\nSecure protocol version: ");
b.append(sslInfo.getProtocolVersion());
b.append("\nCipher suite: ");
b.append(sslInfo.getCipherSuite());
if (cert == null) {
b.append("\nNo server Certificate.");
} else {
b.append("\nServer certificate \n\t Type: ");
b.append(cert.getType());
b.append("\n\t Version: ");
b.append(cert.getVersion());
b.append("\n\t Serial number: ");
b.append(cert.getSerialNumber());
b.append("\n\t Issuer: ");
b.append(cert.getIssuer());
b.append("\n\t Subject: ");
b.append(cert.getSubject());
b.append("\n\t Signature algorithm: ");
b.append(cert.getSigAlgName());
b.append("\n\t Not valid before: ");
b.append(time2str(cert.getNotBefore()));
b.append("\n\t Not valid after: ");
b.append(time2str(cert.getNotAfter()));
}
}
// if not connected getResponseCode should connect
b.append("\nResponseCode: ");
b.append(c.getResponseCode());
b.append("\nResponseMessage:");
b.append(c.getResponseMessage());
b.append("\nContentLength: ");
b.append(c.getLength());
b.append("\nContentType: ");
b.append(c.getType());
b.append("\nContentEncoding: ");
b.append(c.getEncoding());
b.append("\nContentExpiration: ");
b.append(c.getExpiration());
b.append("\nDate: ");
b.append(c.getDate());
b.append("\nLast-Modified: ");
b.append(c.getLastModified());
b.append("\n\n");
int h = 0;
while (true) {
try {
String key = c.getHeaderFieldKey(h);
if (key == null) {
break;
}
String value = c.getHeaderField(h);
b.append(key);
b.append(": ");
b.append(value);
b.append("\n");
h++;
} catch (Exception e) {
// "Exception while fetching headers");
break;
}
}
t = new TextBox("Http Test", b.toString(), b.length(), 0);
setCommands(t, false);
display.setCurrent(t);
} finally {
c.close();
}
} catch (ConnectionNotFoundException e) {
t = new TextBox("Error", "Could not Connect.", 128, 0);
setCommands(t, false);
display.setCurrent(t);
return;
} catch (CertificateException e) {
StringBuffer m = new StringBuffer(256);
String s;
Certificate cert = e.getCertificate();
m.append(e.getMessage());
if (cert != null) {
m.append("\nServer certificate \n\t Type: ");
m.append(cert.getType());
m.append("\n\t Version: ");
m.append(cert.getVersion());
m.append("\n\t Serial number: ");
m.append(cert.getSerialNumber());
m.append("\n\t Issuer: ");
m.append(cert.getIssuer());
m.append("\n\t Subject: ");
m.append(cert.getSubject());
m.append("\n\t Signature algorithm: ");
m.append(cert.getSigAlgName());
m.append("\n\t Not valid before: ");
m.append(time2str(cert.getNotBefore()));
m.append("\n\t Not valid after: ");
m.append(time2str(cert.getNotAfter()));
}
s = m.toString();
t = new TextBox("Certificate Error", s, s.length(), 0);
setCommands(t, false);
display.setCurrent(t);
return;
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Set the funtion to perform based on commands selected.
* @param d Displayable object
* @param islist flag to indicate list processing
*/
void setCommands(Displayable d, boolean islist) {
if (islist) {
d.addCommand(addCommand);
d.addCommand(okCommand);
} else {
d.addCommand(exitCommand);
d.addCommand(chooseCommand);
d.addCommand(getCommand);
d.addCommand(postCommand);
d.addCommand(headCommand);
}
d.setCommandListener(this);
}
/**
* Pause signals the thread to stop by clearing the thread field.
* If stopped before done with the iterations it will
* be restarted from scratch later.
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything. The thread is signaled
* to stop and no result is produced.
* @param unconditional Flag to indicate that forced shutdown
* is requested
*/
public void destroyApp(boolean unconditional) {
}
/**
* Respond to commands, including exit
* @param c command to perform
* @param s Screen displayable object
*/
public void commandAction(Command c, Displayable s) {
synchronized (this) {
if (commandThread != null) {
// process only one command at a time
return;
}
currentCommand = c;
commandThread = new Thread(this);
commandThread.start();
}
}
/**
* Perform the current command set by the method commandAction.
*/
public void run() {
if (currentCommand == exitCommand) {
destroyApp(false);
notifyDestroyed();
} else if (currentCommand == getCommand) {
readContents(HttpConnection.GET);
} else if (currentCommand == postCommand) {
readContents(HttpConnection.POST);
} else if (currentCommand == headCommand) {
readHeaders(HttpConnection.HEAD);
} else if (currentCommand == chooseCommand) {
chooseScreen();
} else if (currentCommand == okCommand) {
int i = list.getSelectedIndex();
if (i >= 0) {
url = list.getString(i);
}
mainScreen();
} else if (currentCommand == addSaveCommand) {
urls.addElement(addTextBox.getString().trim());
chooseScreen();
} else if (currentCommand == addCommand) {
addScreen();
} else if (currentCommand == cancelCommand) {
chooseScreen();
}
synchronized (this) {
// signal that another command can be processed
commandThread = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -