Чистая версия Objective C ...
NSString * duration = @ "P1DT10H15M49S"; int i = 0, days = 0, hours = 0, minutes = 0, seconds = 0; while (i & lt; duration.length) {NSString * str = [duration substringWithRange: NSMakeRange (i, duration.length-i)]; я ++; if ([str hasPrefix: @ "P"] || [str hasPrefix: @ "T"]) продолжить; NSScanner * sc = [NSScanner scannerWithString: str]; int value = 0; if ([sc scanInt: & amp; value]) {i + = [sc scanLocation] -1; str = [duration substringWithRange: NSMakeRange (i, duration.length-i)]; я ++; if ([str hasPrefix: @ "D"]) days = value; else if ([str hasPrefix: @ "H"]) hours = value; else if ([str hasPrefix: @ "M"]) minutes = value; else if ([str hasPrefix: @ "S"]) seconds = value; }} NSLog (@ "% @", [NSString stringWithFormat: @ "% d дней,% d часов,% d mins,% d секунд", дней, часов, минут, секунд]);
Я разработал решение, которое выглядит легче, чем то, что было опубликовано здесь
private String executeGet(final String https_url, final String proxyName, final int port) {
String ret = "";
URL url;
try {
HttpsURLConnection con;
url = new URL(https_url);
if (proxyName.isEmpty()) {
con = (HttpsURLConnection) url.openConnection();
} else {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyName, port));
con = (HttpsURLConnection) url.openConnection(proxy);
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication(USERNAME, PASSWORD.toCharArray()));
}
};
Authenticator.setDefault(authenticator);
}
ret = getContent(con);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
это мой быстрый и грязный клиент https в Java, который игнорирует недопустимые сертификаты и аутентификации с использованием BASIC
import java.io.IOException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
public static HttpsURLConnection getConnection(boolean ignoreInvalidCertificate, String user, String pass, HttpRequestMethod httpRequestMethod, URL url) throws KeyManagementException, NoSuchAlgorithmException, IOException{
SSLContext ctx = SSLContext.getInstance("TLS");
if (ignoreInvalidCertificate){
ctx.init(null, new TrustManager[] { new InvalidCertificateTrustManager() }, null);
}
SSLContext.setDefault(ctx);
String authStr = user+":"+pass;
String authEncoded = Base64.encodeBytes(authStr.getBytes());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + authEncoded);
if (ignoreInvalidCertificate){
connection.setHostnameVerifier(new InvalidCertificateHostVerifier());
}
return connection;
}
-
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
public class InvalidCertificateHostVerifier implements HostnameVerifier{
@Override
public boolean verify(String paramString, SSLSession paramSSLSession) {
return true;
}
}
-
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
/**
* ignore invalid Https certificate from OPAM
* <p>see http://javaskeleton.blogspot.com.br/2011/01/avoiding-sunsecurityvalidatorvalidatore.html
*/
public class InvalidCertificateTrustManager implements X509TrustManager{
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException {
}
}
Возможно, это то, с чего вы можете начать.
, конечно, поскольку у вас есть соединение, вы можете получить содержимое ответа с помощью
InputStream content = (InputStream) connection.getInputStream();