Package java.net

Examples of java.net.HttpURLConnection


            URL url = new URL(to.getAddress().getValue());
            connection = getConnection(url);
            connection.setDoOutput(true);
   
            if (connection instanceof HttpURLConnection) {
                HttpURLConnection hc = (HttpURLConnection)connection;
                hc.setRequestMethod("POST");
            }
        }
        origOut = new WrappedOutputStream();
        out = origOut;
    }
View Full Code Here


        }
         
      }
     
      URL url = new URL(connect_address);
      HttpURLConnection httpConnection =(HttpURLConnection) url.openConnection();
     
      httpConnection.setRequestProperty("User-Agent",     JMUpdater.USER_AGENT);
      httpConnection.setRequestProperty("Accept-Charset", JMUpdater.ENCODING);
     
      httpConnection.setDoInput(true);
      httpConnection.setDoOutput(true);
      if (post_data.length()!=0)
        httpConnection.getOutputStream().write(post_data.getBytes());
     
      httpConnection.connect();
      http_response_code = httpConnection.getResponseCode();
      String result = "";
      InputStreamReader bufIn = new InputStreamReader(httpConnection.getInputStream(),JMUpdater.ENCODING);
            int c;
            while(true) {
              c= bufIn.read();
              if (c==-1) break;
              result+=((char)c);
            }
            httpConnection.disconnect();
      return result;
    }catch(Throwable t) {
      throw new JMHTTPConnectionException(t);
    }
   
View Full Code Here

    protected URLConnection getConnection(URL url) throws IOException {
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        if (connection instanceof HttpURLConnection) {
            HttpURLConnection hc = (HttpURLConnection)connection;
            hc.setRequestMethod("POST");
        }
        connection.setRequestProperty("Content-Type", "text/xml");
        return connection;
    }
View Full Code Here

    }

    protected static int getResponseCode(URLConnection connection) throws IOException {
        int responseCode = HttpURLConnection.HTTP_OK;
        if (connection instanceof HttpURLConnection) {
            HttpURLConnection hc = (HttpURLConnection)connection;
            responseCode = hc.getResponseCode();
        } else {
            if (connection.getHeaderField(HTTP_RESPONSE_CODE) != null) {
                responseCode =
                    Integer.parseInt(connection.getHeaderField(HTTP_RESPONSE_CODE));
            }
View Full Code Here

      } catch (IOException e) {
         // since the error content (normally the html code) is not propagated
         // we need to doit by our self.
         if (getURLConnection() instanceof HttpURLConnection) {
            try {
               HttpURLConnection errUrl = (HttpURLConnection)getURLConnection();
               if (errUrl != null) {
                  InputStream errStr = errUrl.getErrorStream();
                  BufferedReader br = new BufferedReader(new InputStreamReader(errStr));
                  StringBuffer buf = new StringBuffer();
                  String line = "";
                  while (line != null) {
                     line = br.readLine();
                     if (line == null)
                        break;
                     buf.append(line).append("\n");
                  }
                  errStr.close();
                  if (buf.length() > 0) {
                     throw new XmlRpcException(errUrl.getResponseCode(), buf.toString(), e);
                  }
               }
            }
            catch (IOException ex) {
               ex.printStackTrace();
View Full Code Here

   {
      StringBuffer ret = null;
      try {
         // connect to the server
         URL url = new URL(urlStr);
         HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

         // send a POST
         urlConnection.setRequestMethod("POST");
         urlConnection.setDoOutput(true);
         OutputStream outStream = urlConnection.getOutputStream();
         byte[] conversionHelper = inputString.getBytes();
         outStream.write(conversionHelper);
         outStream.flush();

         // read the answer of the server and store it in a string
         ret = new StringBuffer();
         InputStream inStream = urlConnection.getInputStream();
         int bytes = 0, deltaBytes = 0;
         int maxBytes = urlConnection.getContentLength();

         //         while ( (bytes = inStream.available()) > 0) {
         while (bytes < maxBytes) {
            deltaBytes = inStream.available();
            byte buffer[] = new byte[deltaBytes];
View Full Code Here

    throws Exception
  {
    System.out.println("Put file to " + location);

    URL url = new URL(location);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("PUT");
    conn.setDoOutput(true);

    RDFFormat dataFormat = RDFFormat.forFileName(file, RDFFormat.RDFXML);
    conn.setRequestProperty("Content-Type", dataFormat.getDefaultMIMEType());

    InputStream dataStream = ProtocolTest.class.getResourceAsStream(file);
    try {
      OutputStream connOut = conn.getOutputStream();

      try {
        IOUtil.transfer(dataStream, connOut);
      }
      finally {
        connOut.close();
      }
    }
    finally {
      dataStream.close();
    }

    conn.connect();

    int responseCode = conn.getResponseCode();

    if (responseCode != HttpURLConnection.HTTP_OK && // 200 OK
        responseCode != HttpURLConnection.HTTP_NO_CONTENT) // 204 NO CONTENT
    {
      String response = "location " + location + " responded: " + conn.getResponseMessage() + " ("
          + responseCode + ")";
      fail(response);
    }
  }
View Full Code Here

  private void delete(String location)
    throws Exception
  {
    URL url = new URL(location);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("DELETE");

    conn.connect();

    int responseCode = conn.getResponseCode();

    if (responseCode != HttpURLConnection.HTTP_OK && // 200 OK
        responseCode != HttpURLConnection.HTTP_NO_CONTENT) // 204 NO CONTENT
    {
      String response = "location " + location + " responded: " + conn.getResponseMessage() + " ("
          + responseCode + ")";
      fail(response);
    }
  }
View Full Code Here

  {
    location += "?query=" + URLEncoder.encode(query, "UTF-8") + "&queryLn=" + queryLn.getName();

    URL url = new URL(location);

    HttpURLConnection conn = (HttpURLConnection)url.openConnection();

    // Request SPARQL-XML formatted results:
    conn.setRequestProperty("Accept", TupleQueryResultFormat.SPARQL.getDefaultMIMEType());

    conn.connect();

    try {
      int responseCode = conn.getResponseCode();
      if (responseCode == HttpURLConnection.HTTP_OK) {
        // Process query results
        return QueryResultIO.parse(conn.getInputStream(), TupleQueryResultFormat.SPARQL);
      }
      else {
        String response = "location " + location + " responded: " + conn.getResponseMessage() + " ("
            + responseCode + ")";
        fail(response);
        throw new RuntimeException(response);
      }
    }
    finally {
      conn.disconnect();
    }
  }
View Full Code Here

      try {
        URL url = new URL(serverURL);

        System.out.println("connecting to the server...");
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setDoOutput(true);
        con.setRequestMethod("POST");

        if (keyString != null) {
          String form = HttpClientUtil.encodeParameter(SesameServer.SHUTDOWN_KEY_PARAM, keyString);
          OutputStream out = con.getOutputStream();
          out.write(form.getBytes("ISO-8859-1"));
        }

        if (HttpClientUtil.is2xx(con.getResponseCode())) {
          System.out.println("shutdown command sent successfully");
        }
        else {
          System.out.println("Shutdown request failed: " + con.getResponseMessage());
          System.exit(3);
        }
      }
      catch (MalformedURLException e) {
        System.err.println("Malformed URL: " + serverURL);
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.