0

I have installed 2 destinations in my SG with HTTPs protocol. 1 is for: enter image description here

another is for: enter image description here

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?

June7
  • 19,874
  • 8
  • 24
  • 34
yin xu
  • 21
  • 1
  • 3
  • "It can not work" is not a helpful description of what you want to happen or what actually did happen. – The Head Rush Jan 16 '18 at 20:53
  • Actually, I just mean this connection solution seem did not work with my cloud host&port in SG, can not connect that real url. – yin xu Jan 17 '18 at 20:20
  • Still not the most useful feedback. Please edit your post to include what you expected to happen and what actually happened instead of what you expected? If an exception was thrown, please include the stack trace. – The Head Rush Jan 17 '18 at 21:27

2 Answers2

0

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) {

    }

}

}
Snohdo
  • 156
  • 5
0

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.

yin xu
  • 21
  • 1
  • 3