⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpresponse.java

📁 用java开发的一些实用的短信通信模块其中包含MD5加密、http发送等信息
💻 JAVA
字号:
package lib.commons.net.http;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.CharEncoding;

import lib.commons.Utils;

public final class HttpResponse {

	private static String _defaultCharset;

	protected String _url;

	protected int _statusCode;

	protected Map _headers;

	protected List _headerNameList;

	protected String _charset;

	protected byte[] _responseBody;

	private String _contentType, _responseBodyAsString;

	protected HttpResponse() {
	}

	public String getUrl() {
		return _url;
	}

	public int getStatusCode() {
		return _statusCode;
	}

	private static final Pattern _charsetPattern = Pattern.compile(
			".*charset\\s*=\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);

	public String getCharset() {
		if (null == _charset) {
			String contentType = getHeader("Content-Type");
			if (!Utils.StringIsNullOrEmpty(contentType)) {
				Matcher m = _charsetPattern.matcher(contentType);
				if (m.matches()) {
					_charset = m.group(1);
				} else {
					_charset = Utils.EMPTY_STRING;
				}
			} else
				_charset = Utils.EMPTY_STRING;
		}
		return _charset;
	}

	public int getContentLength() {
		return null == _responseBody ? 0 : _responseBody.length;
	}

	public String getContentType() {
		if (null == _contentType) {
			_contentType = getHeader("Content-Type");
			if (!Utils.StringIsNullOrEmpty(_contentType)) {
				int pos = _contentType.indexOf(";");
				if (pos > -1)
					_contentType = _contentType.substring(0, pos);
			} else {
				_contentType = Utils.EMPTY_STRING;
			}
		}
		return _contentType;
	}

	public byte[] getResponseBody() {
		return _responseBody;
	}

	public String getResponseBodyAsString() {
		if (null == _responseBodyAsString) {
			if (null == _responseBody || _responseBody.length == 0)
				_responseBodyAsString = Utils.EMPTY_STRING;
			else {
				try {
					if (!Utils.StringIsNullOrEmpty(this.getCharset())
							&& CharEncoding.isSupported(this.getCharset())) {
						_responseBodyAsString = new String(_responseBody, this
								.getCharset());
					} else {
						if (CharEncoding.isSupported(getDefaultCharset()))
							_responseBodyAsString = new String(_responseBody,
									getDefaultCharset());
						else
							_responseBodyAsString = new String(_responseBody);
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				}

				if ("text/vnd.wap.wml".equalsIgnoreCase(getContentType())) {
					_responseBodyAsString = _responseBodyAsString.replace(
							" ", " ");
					_responseBodyAsString = _responseBodyAsString.replaceFirst(
							"(<!DOCTYPE.*?>)", "");
					try {
						lib.commons.config.DomConfig respXmlDom = lib.commons.config.DomConfig
								.loadXml(_responseBodyAsString);
						if (null != respXmlDom && !respXmlDom.isEmpty())
							_responseBodyAsString = respXmlDom.getXmlString();
					} catch (Exception ex) {

					}
				}
			}
		}
		return _responseBodyAsString;
	}

	public Iterator getHeaderNameIterator() {
		return null == _headerNameList ? null : _headerNameList.iterator();
	}

	public String getHeader(String headerName) {
		String header = null;
		if (null != _headers && !Utils.StringIsNullOrEmpty(headerName)
				&& _headers.containsKey(headerName.toUpperCase())) {
			header = (String) _headers.get(headerName.toUpperCase());
		}
		return header;
	}

	public static Pattern p = Pattern.compile(
			"<(img|image)[^>]+src=['\"]([^'\"]+)[\"']",
			Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

	public String[] getImageUrlsFromBody() {
		String[] imgUrls = null;
		java.util.List urlList = new java.util.ArrayList();
		java.util.regex.Matcher m = p.matcher(this.getResponseBodyAsString());
		lib.commons.net.Url url = lib.commons.net.Url.parseUrl(this.getUrl());
		while (m.find()) {
			String imgUrl = m.group(2);
			if (null != imgUrl)
				imgUrl = imgUrl.replace("&amp;", "&");
			imgUrl = url.getRelativeUrlPath(imgUrl).toString();
			urlList.add(imgUrl);
		}
		if (urlList.size() > 0) {
			imgUrls = new String[urlList.size()];
			for (int i = 0; i < imgUrls.length; i++) {
				imgUrls[i] = (String) urlList.get(i);
			}
		}
		return imgUrls;
	}

	public String getRelativeUrl(String relativeUrl) {
		lib.commons.net.Url url = lib.commons.net.Url.parseUrl(this.getUrl());
		return url.getRelativeUrlPath(relativeUrl).toString();
	}

	public static void setDefaultCharset(String charset) {
		_defaultCharset = charset;
	}

	public static String getDefaultCharset() {
		return null == _defaultCharset ? "UTF-8" : _defaultCharset;
	}

	public static void main(String[] args) throws Exception {
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -