Reputation: 21
I have installed 2 destinations in my SG with HTTPs protocol. 1 is for:
another is for:
from my application, I want to access these 2 url like this:
url = new URL(urlStr);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setReadTimeout(5000);
httpConn.setConnectTimeout(5000);
if(httpConn.getResponseCode() == 200){
inStream = httpConn.getInputStream();
bytesData = IOUtils.toByteArray(inStream);
}
btw, I set the cloud host&port in SG into "urlStr".But it can not work.So anyone can help on a hint?
Upvotes: 0
Views: 59
Reputation: 21
it works as below:
because real url is set up in destination with https protocol, so in code, I update my code using url with cloud host(https) and port, then it works.
Upvotes: 0
Reputation: 156
I fleshed out the code a bit further and got it to work. Perhaps this extended code sample will help you. I would suggest putting in the URL parameter the address starting with https and without a port number as it will default to 443. You can run this with with a classpath similar to java -cp commons-io-2.6/commons-io-2.6.jar:. javassl
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.commons.io.IOUtils;
class javassl {
public static void main(String args[]){
String urlStr = new String("https://www.example.com/");
URL url;
HttpURLConnection httpConn;
InputStream inStream;
byte[] bytesData;
try {
url = new URL(urlStr);
try {
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setReadTimeout(5000);
httpConn.setConnectTimeout(5000);
if(httpConn.getResponseCode() == 200){
inStream = httpConn.getInputStream();
bytesData = IOUtils.toByteArray(inStream);
System.out.println("Got 200 OK bytes " + bytesData.length);
}
} catch (IOException e) {
}
} catch(MalformedURLException e) {
}
}
}
Upvotes: 0