📄 httpclient.java
字号:
/** Invoke HTTP request POST. */
public InputStream postStream() throws HttpClientException {
return sendHttpRequestStream("post");
}
/** Returns the value of the specified named response header field. */
public String getResponseHeader(String header) throws HttpClientException {
if (con == null) {
throw new HttpClientException("Connection not yet established");
}
return con.getHeaderField(header);
}
/** Returns the key for the nth response header field. */
public String getResponseHeaderFieldKey(int n) throws HttpClientException {
if (con == null) {
throw new HttpClientException("Connection not yet established");
}
return con.getHeaderFieldKey(n);
}
/** Returns the value for the nth response header field. It returns null of there are fewer then n fields. */
public String getResponseHeaderField(int n) throws HttpClientException {
if (con == null) {
throw new HttpClientException("Connection not yet established");
}
return con.getHeaderField(n);
}
/** Returns the content of the response. */
public Object getResponseContent() throws java.io.IOException, HttpClientException {
if (con == null) {
throw new HttpClientException("Connection not yet established");
}
return con.getContent();
}
/** Returns the content-type of the response. */
public String getResponseContentType() throws HttpClientException {
if (con == null) {
throw new HttpClientException("Connection not yet established");
}
return con.getContentType();
}
/** Returns the content length of the response */
public int getResponseContentLength() throws HttpClientException {
if (con == null) {
throw new HttpClientException("Connection not yet established");
}
return con.getContentLength();
}
/** Returns the content encoding of the response. */
public String getResponseContentEncoding() throws HttpClientException {
if (con == null) {
throw new HttpClientException("Connection not yet established");
}
return con.getContentEncoding();
}
private String sendHttpRequest(String method) throws HttpClientException {
InputStream in = sendHttpRequestStream(method);
if (in == null) return null;
StringBuffer buf = new StringBuffer();
try {
if (Debug.verboseOn() || debug) {
try {
Debug.log("ContentEncoding: " + con.getContentEncoding() + "; ContentType: " +
con.getContentType() + " or: " + URLConnection.guessContentTypeFromStream(in), module);
} catch (IOException ioe) {
Debug.logWarning(ioe, "Caught exception printing content debugging information", module);
}
}
String charset = null;
String contentType = con.getContentType();
if (contentType == null) {
try {
contentType = URLConnection.guessContentTypeFromStream(in);
} catch (IOException ioe) {
Debug.logWarning(ioe, "Problems guessing content type from steam", module);
}
}
if (Debug.verboseOn() || debug) Debug.log("Content-Type: " + contentType, module);
if (contentType != null) {
contentType = contentType.toUpperCase();
int charsetEqualsLoc = contentType.indexOf("=", contentType.indexOf("CHARSET"));
int afterSemiColon = contentType.indexOf(";", charsetEqualsLoc);
if (charsetEqualsLoc >= 0 && afterSemiColon >= 0) {
charset = contentType.substring(charsetEqualsLoc + 1, afterSemiColon);
} else if (charsetEqualsLoc >= 0) {
charset = contentType.substring(charsetEqualsLoc + 1);
}
if (charset != null) charset = charset.trim();
if (Debug.verboseOn() || debug) Debug.log("Getting text from HttpClient with charset: " + charset, module);
}
BufferedReader post = new BufferedReader(charset == null ? new InputStreamReader(in) : new InputStreamReader(in, charset));
String line = new String();
if (Debug.verboseOn() || debug) Debug.log("---- HttpClient Response Content ----", module);
while ((line = post.readLine()) != null) {
if (Debug.verboseOn() || debug) Debug.log("[HttpClient] : " + line, module);
buf.append(line);
if (lineFeed) {
buf.append("\n");
}
}
} catch (Exception e) {
throw new HttpClientException("Error processing input stream", e);
}
return buf.toString();
}
private InputStream sendHttpRequestStream(String method) throws HttpClientException {
// setup some SSL variables
SSLUtil.loadJsseProperties();
String arguments = null;
InputStream in = null;
if (url == null) {
throw new HttpClientException("Cannot process a null URL.");
}
if (rawStream != null) {
arguments = rawStream;
} else if (parameters != null && parameters.size() > 0) {
arguments = UtilHttp.urlEncodeArgs(parameters);
}
// Append the arguments to the query string if GET.
if (method.equalsIgnoreCase("get") && arguments != null) {
url = url + "?" + arguments;
}
// Create the URL and open the connection.
try {
requestUrl = new URL(url);
con = URLConnector.openConnection(requestUrl, timeout, clientCertAlias);
if (Debug.verboseOn() || debug) Debug.log("Connection opened to : " + requestUrl.toExternalForm(), module);
if ((con instanceof HttpURLConnection)) {
((HttpURLConnection) con).setInstanceFollowRedirects(followRedirects);
if (Debug.verboseOn() || debug) Debug.log("Connection is of type HttpURLConnection", module);
}
con.setDoOutput(true);
con.setUseCaches(false);
if (Debug.verboseOn() || debug) Debug.log("Do Input = true / Use Caches = false", module);
if (method.equalsIgnoreCase("post")) {
con.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
con.setDoInput(true);
if (Debug.verboseOn() || debug) Debug.log("Set content type to : application/x-www-form-urlencoded", module);
}
if (headers != null && headers.size() > 0) {
Set headerSet = headers.keySet();
Iterator i = headerSet.iterator();
while (i.hasNext()) {
String headerName = (String) i.next();
String headerValue = (String) headers.get(headerName);
con.setRequestProperty(headerName, headerValue);
if (Debug.verboseOn() || debug) Debug.log("Header : " + headerName + " -> " + headerValue, module);
}
} else {
if (Debug.verboseOn() || debug) Debug.log("No headers to set", module);
}
if (method.equalsIgnoreCase("post")) {
DataOutputStream out = new DataOutputStream(con.getOutputStream());
if (Debug.verboseOn() || debug) Debug.log("Opened output stream", module);
out.writeBytes(arguments);
if (Debug.verboseOn() || debug) Debug.log("Wrote arguements (parameters) : " + arguments, module);
out.flush();
out.close();
if (Debug.verboseOn() || debug) Debug.log("Flushed and closed buffer", module);
}
if (Debug.verboseOn() || debug) {
Map headerFields = con.getHeaderFields();
Debug.log("Header Fields : " + headerFields, module);
}
in = con.getInputStream();
} catch (IOException ioe) {
throw new HttpClientException("IO Error processing request", ioe);
} catch (Exception e) {
throw new HttpClientException("Error processing request", e);
}
return in;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -