Package java.net

Examples of java.net.HttpURLConnection


      sParams += aParams[p].getName()+"="+URLEncoder.encode(aParams[p].getValue(), "UTF-8");
      if (p<aParams.length-1) sParams += "&";
    } // next
  } // fi

    HttpURLConnection oCon = (HttpURLConnection) oUrl.openConnection();
   
    oCon.setUseCaches(false);
    oCon.setFollowRedirects(false);
    oCon.setInstanceFollowRedirects(false);
    oCon.setDoInput (true);

    oCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  oCon.setRequestProperty("Content-Length", String.valueOf(sParams.getBytes().length));
  oCon.setFixedLengthStreamingMode(sParams.getBytes().length);
    oCon.setDoOutput(true);
  oCon.setRequestMethod("POST");
  OutputStreamWriter oWrt = new OutputStreamWriter(oCon.getOutputStream());
    oWrt.write(sParams);
    oWrt.flush();
    oWrt.close();

  int responseCode = oCon.getResponseCode();

  if (responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
    responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {

      HttpRequest oMoved = new HttpRequest(oCon.getHeaderField("Location"), oUrl, "POST", aParams);   
    oRetVal = oMoved.post();
    sUrl = oMoved.url();
  } else if (responseCode == HttpURLConnection.HTTP_OK ||
      responseCode == HttpURLConnection.HTTP_ACCEPTED) {
    InputStream oStm = oCon.getInputStream();
    String sEnc = oCon.getContentEncoding();
    if (sEnc==null) {
      ByteArrayOutputStream oBya = new ByteArrayOutputStream();
      new StreamPipe().between(oStm, oBya);
      oRetVal = oBya.toByteArray();
    } else {
      int c;
      StringBuffer oDoc = new StringBuffer();
      Reader oRdr = new InputStreamReader(oStm, sEnc);
      while ((c=oRdr.read())!=-1) {
        oDoc.append((char) c);
      } // wend
      oRdr.close();
      oRetVal = oDoc.toString();
    }
    oStm.close();
  } else {
    throw new IOException(String.valueOf(responseCode));
  }
  oCon.disconnect();
  return oRetVal;
  } // post
View Full Code Here


  if (null==oReferUrl)
    oUrl = new URL(sUrl);
  else
    oUrl = new URL(oReferUrl, sUrl);

    HttpURLConnection oCon = (HttpURLConnection) oUrl.openConnection();
   
    oCon.setUseCaches(false);
    oCon.setFollowRedirects(false);
    oCon.setInstanceFollowRedirects(false);
    oCon.setDoInput (true);

    oCon.setDoOutput(true);
  oCon.setRequestMethod("GET");
  OutputStreamWriter oWrt = new OutputStreamWriter(oCon.getOutputStream());
    oWrt.flush();
    oWrt.close();

  int responseCode = oCon.getResponseCode();

  if (responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
    responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
      HttpRequest oMoved = new HttpRequest(oCon.getHeaderField("Location"), oUrl, "GET", null);
    oRetVal = oMoved.get();
    sUrl = oMoved.url();
  } else if (responseCode == HttpURLConnection.HTTP_OK ||
      responseCode == HttpURLConnection.HTTP_ACCEPTED) {
    InputStream oStm = oCon.getInputStream();
    String sEnc = oCon.getContentEncoding();
    if (sEnc==null) {
      ByteArrayOutputStream oBya = new ByteArrayOutputStream();
      new StreamPipe().between(oStm, oBya);
      oRetVal = oBya.toByteArray();
    } else {
      int c;
      StringBuffer oDoc = new StringBuffer();
      Reader oRdr = new InputStreamReader(oStm, sEnc);
      while ((c=oRdr.read())!=-1) {
        oDoc.append((char) c);
      } // wend
      oRdr.close();
      oRetVal = oDoc.toString();
    }
    oStm.close();
  } else {
    throw new IOException(String.valueOf(responseCode));
  }
  oCon.disconnect();
  return oRetVal;
  } // post
View Full Code Here

    if (DebugFile.trace) {
      DebugFile.writeln("new URL("+sUrl+"username="+sUsr+"&password=..."+sFrm+"&to="+sMsisdn+"&text=...)");
    }

    URL oUrl = new URL(sQry);
      HttpURLConnection oCon = (HttpURLConnection) oUrl.openConnection();
      int iStatusCode = oCon.getResponseCode();

    if (DebugFile.trace) {
      DebugFile.writeln("response code is "+String.valueOf(iStatusCode));
    }

      switch (iStatusCode) {
        case 200:
          oRdr = new BufferedReader(new InputStreamReader(oCon.getInputStream(), "UTF-8"));
        sLin = oRdr.readLine();
            while( null != sLin ) {
              sLin = oRdr.readLine();
            } // wend
        oRdr.close();
        oRsp = new SMSResponse(oMsg.messageId(), new Date(), SMSResponse.ErrorCode.NONE, SMSResponse.StatusCode.POSITIVE_ACK, "");
        break;
        case 422:
          SMSResponse.ErrorCode eErr = SMSResponse.ErrorCode.UNKNOWN_ERROR;
          oRdr = new BufferedReader(new InputStreamReader(oCon.getErrorStream(), "UTF-8"));
        sErr = "";
        sLin = oRdr.readLine();
            while( null != sLin ) {
              if (sLin.indexOf("número de móvil válido")>0) eErr = SMSResponse.ErrorCode.INVALID_MSISDN;
              if (sLin.indexOf("es demasiado largo")>0) eErr = SMSResponse.ErrorCode.TEXT_TOO_LONG;
              if (sLin.indexOf("Usuario o contraseña erróneos")>0) eErr = SMSResponse.ErrorCode.AUTHENTICATION_FAILURE;
              sErr += sLin + "\n";
              sLin = oRdr.readLine();
            } // wend
        oRdr.close();
        oRsp = new SMSResponse(oMsg.messageId(), new Date(), eErr, SMSResponse.StatusCode.NEGATIVE_FAILED_DELIVERY, sErr);
        break;
        default:
            throw new IOException("Invalid HTTP response Code "+String.valueOf(iStatusCode));
      } // end switch 
   
    oCon.disconnect();

    if (DebugFile.trace) {
      DebugFile.decIdent();
      DebugFile.writeln("End SMSPushRealidadFutura.push() : " + oRsp.toString());
    }
View Full Code Here

      } // next
    } // fi (aFiles)
  } else if (sSourceURI.startsWith("http://")) {
    try {
        URL oUrl = new URL(sSourceURI);
      HttpURLConnection oCon = (HttpURLConnection) oUrl.openConnection();
      int iResponseCode = oCon.getResponseCode();
      oCon.disconnect();
      bExists = (iResponseCode>=200 && iResponseCode<=299);
      } catch (MalformedURLException mue) {
        throw new IOException(mue.getMessage());
      }
  } else if (sSourceURI.startsWith("https://")) {
    try {
        URL oUrl = new URL(sSourceURI);
      HttpsURLConnection oCon = (HttpsURLConnection) oUrl.openConnection();
      int iResponseCode = oCon.getResponseCode();
      oCon.disconnect();
      bExists = (iResponseCode>=200 && iResponseCode<=299);
      } catch (MalformedURLException mue) {
        throw new IOException(mue.getMessage());
      }
  }
View Full Code Here

  {
    int httpReturnCode = -1;
   
    // Try to provide the HTTP response code
    if (conn instanceof HttpURLConnection) {
      HttpURLConnection hconn = (HttpURLConnection) conn;
      try {
        httpReturnCode = hconn.getResponseCode();
        message += " (HTTP response code: " + httpReturnCode + ")";
       
      }
      catch (Exception exc) {}
    }
View Full Code Here

    String methodName, Object[] arguments, Type returnType,
    Map<String, String> extraHeaders)
    throws Exception {

    // create URLConnection
    HttpURLConnection con = (HttpURLConnection)serviceUrl.openConnection(connectionProxy);
    con.setConnectTimeout(connectionTimeoutMillis);
    con.setReadTimeout(readTimeoutMillis);
    con.setAllowUserInteraction(false);
    con.setDefaultUseCaches(false);
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setInstanceFollowRedirects(true);
    con.setRequestMethod("POST");
   
    // add headers
    for (Entry<String, String> entry : headers.entrySet()) {
      con.setRequestProperty(entry.getKey(), entry.getValue());
    }
    for (Entry<String, String> entry : extraHeaders.entrySet()) {
      con.setRequestProperty(entry.getKey(), entry.getValue());
    }
    con.setRequestProperty("Content-Type", "application/json-rpc");
   
    // open the connection
    con.connect();

    // invoke it
    super.invoke(methodName, arguments, con.getOutputStream());

    // read and return value
    return super.readResponse(returnType, con.getInputStream());
  }
View Full Code Here

                            urlCon.setDoOutput(true);
                            urlCon.setUseCaches(false); // Enable tunneling.
                            if (urlCon instanceof HttpURLConnection) {
                                // The DOM L3 LS CR says if we are writing to an HTTP URI
                                // it is to be done with an HTTP PUT.
                                HttpURLConnection httpCon = (HttpURLConnection) urlCon;
                                httpCon.setRequestMethod("PUT");
                            }
                            out = urlCon.getOutputStream();
                        }
                        ser.setOutputByteStream(out);
                    }
View Full Code Here

                urlCon.setDoOutput(true);
                urlCon.setUseCaches(false); // Enable tunneling.
                if (urlCon instanceof HttpURLConnection) {
                    // The DOM L3 LS CR says if we are writing to an HTTP URI
                    // it is to be done with an HTTP PUT.
                    HttpURLConnection httpCon = (HttpURLConnection) urlCon;
                    httpCon.setRequestMethod("PUT");
                }
                out = urlCon.getOutputStream();
            }
            ser.setOutputByteStream(out);
View Full Code Here

    }

    private Object invoke(RemoteInvocation invocation)
            throws Exception {

        HttpURLConnection conn = (HttpURLConnection) remoteURL.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestProperty("ContentType", REQUEST_CONTENT_TYPE);
        conn.setRequestMethod("POST");
        OutputStream os = conn.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        try {
            oos.writeObject(invocation);
            oos.flush();
        } catch (ObjectStreamException e) {
            throw new RuntimeException(e);
        }

        /* read the response */
        InputStream is = conn.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        InvocationResult result = (InvocationResult) ois.readObject();

        ois.read(); // jsse connection pooling hack
        ois.close();
View Full Code Here

 
    if (conn != null)
    {
      if (conn instanceof HttpURLConnection)
      {
        final HttpURLConnection huc = (HttpURLConnection) conn;
        huc.disconnect();
      }
      // TODO: others
    }
   
    connected = false;
View Full Code Here

TOP

Related Classes of java.net.HttpURLConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.