프록시를 사용하여 서버에서 정보를 검색하는 다음 방법이 있습니다. 때로는 인해 당신은 내가 서버가 아무것도 반환하면 서버 응답의 내용을 얻기 위해 그렇게 할 필요가 이미 catch (IOException ioe)
을 사용하고 내 방법에서 볼 수 있듯이 내가 SocketException
, SSLException
, SSLHandshakeException
또는 ConnectException
특정 예외에서 다시 시도하십시오.
을 얻을 나쁜 프록시
위의 예외가 발생한 경우 어떻게 메소드를 다시 시도 할 수 있습니까?
public String getMeta() throws IOException
{
HttpsURLConnection con = null;
InputStream is = null;
StringWriter writer = new StringWriter();
try
{
String url = "https://api.myapp.com/meta";
URL urlObj = new URL(url);
if (useProxy && fullProxy)
{
myapp.Proxy proxyCustom = getRandomProxy();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyCustom.getProxyIp(), proxyCustom.getProxyPort()));
con = (HttpsURLConnection) urlObj.openConnection(proxy);
}
else
{
con = (HttpsURLConnection) urlObj.openConnection();
}
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
con.setRequestProperty("host", urlObj.getHost());
con.setRequestProperty("Connection", "Keep-Alive");
int responseCode = 0;
responseCode = con.getResponseCode();
is = con.getInputStream();
writer = new StringWriter();
IOUtils.copy(is, writer, "UTF-8");
}
catch (IOException ioe)
{
if (con instanceof HttpsURLConnection)
{
HttpsURLConnection httpConn = (HttpsURLConnection) con;
int statusCode = httpConn.getResponseCode();
if (statusCode != 200)
{
is = httpConn.getErrorStream();
writer = new StringWriter();
IOUtils.copy(is, writer, "UTF-8");
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return writer.toString();
}
루프 또는 재귀를 사용하십시오. 여분의 로직을 사용하지 않고 try 블록에서부터 새로운 메소드로 모든 것을 넣으십시오. 그러면 다른 시간에 전화 할 수 있습니다. – HopefullyHelpful
'메소드 재시도를 어떻게 만들 수 있습니까? '한 번 재시도 하시겠습니까? 아니면 예외가 발생하지 않을 때까지 계속 재 시도 하시겠습니까? – copeg
@copeg 재 시도해야하는 시간을 설정하는 간단한 방법이 있기를 바랍니다.하지만 너무 복잡하기 때문에 한 번만 시도하면됩니다. – Arya