public static String httsRequest(String url, String contentdata) {
    String str_return = "";
    SSLContext sc = null;
    try {
      sc = SSLContext.getInstance("SSL");
    } catch (NoSuchAlgorithmException e) {

      e.printStackTrace();
    }
    try {
      sc.init(
          null, new TrustManager[] {new TrustAnyTrustManager()}, new java.security.SecureRandom());
    } catch (KeyManagementException e) {

      e.printStackTrace();
    }
    URL console = null;
    try {
      console = new URL(url);
    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    HttpsURLConnection conn;
    try {
      conn = (HttpsURLConnection) console.openConnection();
      conn.setRequestMethod("POST");
      conn.setSSLSocketFactory(sc.getSocketFactory());
      conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
      conn.setRequestProperty("Accept", "application/json");
      conn.setDoInput(true);
      conn.setDoOutput(true);
      // contentdata="username=arcgis&password=arcgis123&client=requestip&f=json"
      String inpputs = contentdata;
      OutputStream os = conn.getOutputStream();
      os.write(inpputs.getBytes());
      os.close();
      conn.connect();
      InputStream is = conn.getInputStream();
      // // DataInputStream indata = new DataInputStream(is);
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      String ret = "";
      while (ret != null) {
        ret = reader.readLine();
        if (ret != null && !ret.trim().equals("")) {
          str_return = str_return + ret;
        }
      }
      is.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return str_return;
  }
Exemplo n.º 2
0
    public void run() {
      try {
        URL url = new URL(protocol + "://localhost:" + port + "/test1/" + f);
        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        if (urlc instanceof HttpsURLConnection) {
          HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
          urlcs.setHostnameVerifier(
              new HostnameVerifier() {
                public boolean verify(String s, SSLSession s1) {
                  return true;
                }
              });
          urlcs.setSSLSocketFactory(ctx.getSocketFactory());
        }
        byte[] buf = new byte[4096];

        if (fixedLen) {
          urlc.setRequestProperty("XFixed", "yes");
        }
        InputStream is = urlc.getInputStream();
        File temp = File.createTempFile("Test1", null);
        temp.deleteOnExit();
        OutputStream fout = new BufferedOutputStream(new FileOutputStream(temp));
        int c, count = 0;
        while ((c = is.read(buf)) != -1) {
          count += c;
          fout.write(buf, 0, c);
        }
        is.close();
        fout.close();

        if (count != size) {
          throw new RuntimeException("wrong amount of data returned");
        }
        String orig = root + "/" + f;
        compare(new File(orig), temp);
        temp.delete();
      } catch (Exception e) {
        e.printStackTrace();
        fail = true;
      }
    }
  protected HttpURLConnection connect(String url) throws Exception {
    HttpMessageContext context = HttpMessageContext.getInstance();
    context.Debug("GNHttpConnection [connect] connect");
    URLConnection uc = null;
    /*
    synchronized(context)
    {
      Properties prop = System.getProperties();*/
    /*NSL20070112
    if(isUsingProxy())
    {
      prop.setProperty("proxySet", "true" );
      prop.setProperty("proxyHost", getConfig_http_proxy_url());
      prop.setProperty("proxyPort", getConfig_http_proxy_port());

      prop.setProperty("http.proxySet","true");
      prop.setProperty("http.proxyHost",getConfig_http_proxy_url());
      prop.setProperty("http.proxyPort",getConfig_http_proxy_port());
      prop.setProperty("https.proxyHost", getConfig_http_proxy_url());
      prop.setProperty("https.proxyPort", getConfig_http_proxy_port());
    }
    else
    {
      prop.remove("proxySet");
      prop.remove("proxyHost");
      prop.remove("proxyPort");

      prop.remove("http.proxySet");
      prop.remove("http.proxyHost");
      prop.remove("http.proxyPort");
      prop.remove("https.proxyHost");
      prop.remove("https.proxyPort");
    }
    Authenticator.setDefault(new AuthImpl());
    */
    /*NSL20070507 No need to change the JVM property
    if(!HttpMessageContext.emptyString(keyStoreFile))
    {
      prop.put("javax.net.ssl.keyStore", keyStoreFile);
      prop.put("javax.net.ssl.keyStorePassword", JavaKeyStoreHandler.getTrustStorePassword(keyStorePassword));
    }
    if (!HttpMessageContext.emptyString(trustStoreFile))
    {
        prop.put("javax.net.ssl.trustStore", trustStoreFile);
        prop.put("javax.net.ssl.trustStorePassword", trustStorePassword);
    }*/
    /*NSL20070112
    context.Debug("GNHttpConnection [connect]" + "Connecting URL(" + url + ") timeout[" + timeout +" s]");
    myURLobject = new URL(url);
    uc = myURLobject.openConnection();
    */
    /*}*/

    context.Debug(
        "GNHttpConnection [connect]" + "Connecting URL(" + url + ") timeout[" + timeout + " s]");
    try {
      myURLobject = new URL(url);
      List<Proxy> proxyList = findProxy(myURLobject.toURI());
      if (proxyList != null && !proxyList.isEmpty()) {
        for (Proxy proxy : proxyList) {
          try {
            uc = myURLobject.openConnection(proxy);
            break;
          } catch (Exception ex) {
          }
        }
      }
      if (uc == null) {
        uc = myURLobject.openConnection();
      }
    } catch (MalformedURLException ex) {
      context.Warn("GNHttpConnection [connect] Malformed URL: " + url, ex);
    } catch (URISyntaxException ex) {
      context.Warn("GNHttpConnection [connect] Invalid URI: " + myURLobject, ex);
    }

    connection = (HttpURLConnection) uc;
    context.Debug("GNHttpConnection [connect] Connection created");
    if (connection instanceof HttpsURLConnection) {
      context.Debug("GNHttpConnection [connect] Https Connection");
      HttpsURLConnection https = (HttpsURLConnection) connection;
      // SSLSocketFactory factory = null;
      SSLSocketFactory factory = getSSLSocketFactory(); // NSL20070507
      if (!authenticateServer) {
        // factory = new NoAuthSSLSocketFactory( https.getSSLSocketFactory() );
        factory = new NoAuthSSLSocketFactory(factory); // NSL20070507
      }

      if (factory != null) {
        https.setSSLSocketFactory(factory);
      }

      if (!verifyServerHostname) {
        https.setHostnameVerifier(new NoVerificationHostnameVerifier());
      }
    }
    return connection;
  }