com.github.dennisit.vplus.data.utils.HttpUtils Maven / Gradle / Ivy
package com.github.dennisit.vplus.data.utils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
public class HttpUtils {
public static Object httpsConvert(Object object) {
if (object != null) {
String text = JSON.toJSONString(object);
if (text.indexOf("http://") > -1) {
text = replaceAllIgnoreCase(text);
object = JSON.parseObject(text, object.getClass());
}
}
return object;
}
private static String replaceAllIgnoreCase(String input) {
Pattern p = Pattern.compile("http://", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(input);
return m.replaceAll("https://");
}
/*public static InputStream downFile(String src) throws IOException {
return downFile(URI.create(src));
}
public static InputStream downFile(URI uri) throws IOException {
HttpResponse httpResponse;
try {
Request request = Request.Get(uri);
HttpHost httpHost = URIUtils.extractHost(uri);
if (StringUtils.isNotEmpty(httpHost.getHostName())) {
request.setHeader("Host", httpHost.getHostName());
}
request.addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
httpResponse = request.execute().returnResponse();
} catch (Exception var4) {
log.error("远程请求失败,url=" + uri, var4);
throw new FileNotFoundException();
}
int code = httpResponse.getStatusLine().getStatusCode();
if (code != 200) {
throw new FileNotFoundException();
} else {
return httpResponse.getEntity().getContent();
}
}*/
}