User Tools

Site Tools


informatica:linux:java:maven

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
informatica:linux:java:maven [2016/06/28 12:54] joseinformatica:linux:java:maven [2017/07/26 11:49] (current) – [Proyecto POST] jose
Line 44: Line 44:
  <version>1.0-SNAPSHOT</version>  <version>1.0-SNAPSHOT</version>
 </project> </project>
 +</code>
 +
 +También podemos añadir la versión de java, me daba algún error porque era la 5 por defecto:
 +<code>
 +<properties>
 +    <maven.compiler.source>1.7</maven.compiler.source>
 +    <maven.compiler.target>1.7</maven.compiler.target>
 +</properties>
 </code> </code>
  
Line 49: Line 57:
   mkdir -p src/main/java/org/iwanttobefreak/post/   mkdir -p src/main/java/org/iwanttobefreak/post/
  
 +
 +El nombre del fichero .java, en este caso Main es la clase que declaramos en el código fuente
   src/main/java/org/iwanttobefreak/post/Main.java   src/main/java/org/iwanttobefreak/post/Main.java
 <code> <code>
Line 95: Line 105:
  
 <code> <code>
-package org.iwanttobefreak.post;+package com.iwanttobefreak.post;
  
-import java.io.*+import java.io.BufferedReader; 
-import java.net.*;+import java.io.DataOutputStream; 
 +import java.io.InputStreamReader; 
 +import java.net.HttpURLConnection
 +import java.net.URL;
  
-public class Main { +import javax.net.ssl.HttpsURLConnection;
-        public static void main(String[] args) throws IOException { +
-String url = "https://www.google.es/search"; +
-String charset = "UTF-8"; +
-String q = "hola";+
  
-String query = String.format("q=%s",URLEncoder.encode(q, charset));+public class HttpURLConnectionExample {
  
-//URLConnection connection = new URL(url).openConnection(); + private final String USER_AGENT = "Mozilla/5.0";
-URLConnection connection = new URL(url + "?" + query).openConnection();+
  
-connection.setDoOutput(true)+ public static void main(String[] argsthrows Exception {
-connection.setRequestProperty("Accept-Charset", charset); +
-connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);+
  
-                try (OutputStream output connection.getOutputStream()) { + HttpURLConnectionExample http new HttpURLConnectionExample(); 
-                        output.write(query.getBytes(charset)); + 
-                        }catch(IOException ioe){ + System.out.println("\nTesting 2 - Send Http POST request")
-                        ioe.printStackTrace(); + http.sendPost()
-                        //return null; + 
-                        +
-                InputStream response = connection.getInputStream(); + 
-        }+ // HTTP POST request 
 + private void sendPost() throws Exception 
 + 
 + String url = "http://example.com/main.do"; 
 + URL obj = new URL(url); 
 + HttpsURLConnection con = (HttpsURLConnection) obj.openConnection()
 + 
 + //add reuqest header 
 + con.setRequestMethod("POST"); 
 + con.setRequestProperty("User-Agent", USER_AGENT); 
 + con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); 
 +                con.setRequestProperty("Content-Type", "application/json"); 
 + 
 +                //Petición JSON si tiene comillas, las trampeamos con \ 
 + String urlParameters = "{\"DatosPersonales\":{\"Nombre\":\"Aitor\",\"Apellido\":\"Tillas\"},\"DatosFisicos\":{\"Ojos\":\"Azules\",\"Pelo\":\"Negro\"}}"; 
 + 
 + // Send post request 
 + con.setDoOutput(true); 
 + DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
 + wr.writeBytes(urlParameters); 
 + wr.flush(); 
 + wr.close(); 
 + 
 + int responseCode = con.getResponseCode(); 
 + System.out.println("\nSending 'POST' request to URL : " + url); 
 + System.out.println("Post parameters : " + urlParameters); 
 + System.out.println("Response Code : " + responseCode); 
 + 
 + BufferedReader in = new BufferedReader( 
 +         new InputStreamReader(con.getInputStream())); 
 + String inputLine; 
 + StringBuffer response = new StringBuffer(); 
 + 
 + while ((inputLine = in.readLine()) != null) { 
 + response.append(inputLine)
 +
 + in.close(); 
 +  
 + //print result 
 + System.out.println(response.toString()); 
 + }
 } }
 </code> </code>
 +
 +====== Proyecto Out Of Memory OOM ======
 +<code>
 +public class TestOOM
 +{
 +public static void main(String ar[])
 +{
 +String test[]=new String[Integer.MAX_VALUE];
 +for(int i=0;i<Integer.MAX_VALUE;i++)
 +{
 +test[i]="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
 +}
 +}
 +}
 +
 +</code>
 +
  
 ====== Monitorizar con javamelody ====== ====== Monitorizar con javamelody ======
informatica/linux/java/maven.1467118477.txt.gz · Last modified: 2016/06/28 12:54 by jose