use of com.crivano.swaggerservlet.SwaggerError in project balcaovirtual by trf2-jus-br.
the class CertidaoEmitirRequisitanteCpfcnpjPost method fetch.
public static FetchResponse fetch(String url, String authorization, String method, String body) throws Exception {
HttpURLConnection con = null;
URL obj = new URL(url);
con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("User-Agent", "SwaggerServlets");
if (authorization != null)
con.setRequestProperty("Authorization", authorization);
con.setRequestMethod(method);
if ("POST".equals(method)) {
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// set timeout to 5 seconds
con.setConnectTimeout(5000);
// set read timeout to 20 seconds
con.setReadTimeout(20000);
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(body.getBytes(StandardCharsets.UTF_8));
wr.flush();
wr.close();
}
con.setInstanceFollowRedirects(false);
int responseCode = con.getResponseCode();
while (responseCode == 302) {
String newUrl = con.getHeaderField("Location");
String cookies = con.getHeaderField("Set-Cookie");
if (!newUrl.startsWith("http://") && !newUrl.startsWith("https://"))
newUrl = url.substring(0, url.lastIndexOf("/") + 1) + newUrl;
URL obj2 = new URL(newUrl);
con = (HttpURLConnection) obj2.openConnection();
con.setRequestProperty("Cookie", cookies);
con.setRequestProperty("User-Agent", "SwaggerServlets");
if (authorization != null)
con.setRequestProperty("Authorization", authorization);
con.setRequestMethod("GET");
responseCode = con.getResponseCode();
}
if (responseCode >= 400 && responseCode < 600) {
SwaggerError err = null;
InputStream errorStream = null;
String string = null;
errorStream = con.getErrorStream();
if (errorStream != null)
string = convertStreamToString(errorStream);
String errormsg = "HTTP ERROR: " + Integer.toString(responseCode) + " - " + string;
if (con.getResponseMessage() != null)
errormsg = errormsg + " - " + con.getResponseMessage();
errormsg = errormsg.replaceAll("\\s+", " ");
throw new PresentableUnloggedException(errormsg);
}
FetchResponse resp = new FetchResponse();
resp.html = convertStreamToString(con.getInputStream());
resp.headerFields = con.getHeaderFields();
return resp;
}
Aggregations