| | |
| | | import java.net.URL;
|
| | | import java.net.URLEncoder;
|
| | | import java.security.KeyStore;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Iterator;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.regex.Matcher;
|
| | | import java.util.regex.Pattern;
|
| | |
|
| | | import javax.net.ssl.SSLContext;
|
| | |
|
| | |
| | | import net.sf.json.JSONObject;
|
| | |
|
| | | public class HttpUtil {
|
| | |
|
| | | public static List<String> getUrlListFromText(String text) {
|
| | | List<String> urlList = new ArrayList<String>();
|
| | | // 链接
|
| | | String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
|
| | | Pattern p = Pattern.compile(URL_REGEX);
|
| | | Matcher matcher = p.matcher(text);
|
| | | while (matcher.find()) {
|
| | | String url = matcher.group();
|
| | | if (!StringUtil.isNullOrEmpty(url)) {
|
| | | urlList.add(url);
|
| | | }
|
| | | }
|
| | | return urlList;
|
| | | }
|
| | |
|
| | | public static String getHost(String host) {
|
| | | try {
|
| | |
| | | return "";
|
| | | }
|
| | |
|
| | | @SuppressWarnings("deprecation")
|
| | | public static String post(String url, String entity, String charset) {
|
| | | HttpClient client = new HttpClient();
|
| | | PostMethod method = new PostMethod(url);
|
| | | method.addRequestHeader("Content-Type", "text/html;charset=" + charset);
|
| | | method.setRequestHeader("Content-Type", "text/html;charset=" + charset);
|
| | | method.setRequestBody(entity);
|
| | | try {
|
| | | client.executeMethod(method);
|
| | | return convertInputStreamToString(method.getResponseBodyAsStream());
|
| | | } catch (HttpException e) {
|
| | | e.printStackTrace();
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return "";
|
| | | }
|
| | |
|
| | | public static String postInputStream(String url, File file) throws FileNotFoundException {
|
| | | HttpClient client = new HttpClient();
|
| | | PostMethod postMethod = new PostMethod(url);
|
| | |
| | |
|
| | | return result;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取post返回location
|
| | | * |
| | | * @param url
|
| | | * @return
|
| | | */
|
| | | public static String getLocation(String url) {
|
| | | HttpClient client = new HttpClient();
|
| | | PostMethod method = new PostMethod(url);
|
| | | try {
|
| | | client.executeMethod(method);
|
| | | return method.getResponseHeader("location").getValue();
|
| | | } catch (HttpException e) {
|
| | | e.printStackTrace();
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | | }
|