/[projects]/dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/util/HttpUtil.java
ViewVC logotype

Diff of /dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/util/HttpUtil.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

dao/DaoAdresseService/src/dk/daoas/daoadresseservice/util/HttpUtil.java revision 2321 by torben, Tue Feb 17 09:06:42 2015 UTC dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/util/HttpUtil.java revision 2618 by torben, Wed Jul 15 13:48:40 2015 UTC
# Line 12  import java.net.URL; Line 12  import java.net.URL;
12  import java.net.URLConnection;  import java.net.URLConnection;
13  import java.net.URLDecoder;  import java.net.URLDecoder;
14  import java.net.URLEncoder;  import java.net.URLEncoder;
15    import java.nio.charset.Charset;
16  import java.util.HashMap;  import java.util.HashMap;
17  import java.util.Map;  import java.util.Map;
18    
# Line 69  public class HttpUtil { Line 70  public class HttpUtil {
70                  StringBuilder sb = new StringBuilder();                  StringBuilder sb = new StringBuilder();
71                                    
72                  boolean isFirst = true;                                          boolean isFirst = true;                        
73                  for (String key :  params.keySet()) {                                    for (Map.Entry<String,String> entry :  params.entrySet()) {                    
74                          if (isFirst) {                          if (isFirst) {
75                                  isFirst = false;                                  isFirst = false;
76                          } else {                          } else {
77                                  sb.append("&");                                  sb.append("&");
78                          }                          }
79                                                    
80                          String val = params.get(key);                          
81                          sb.append(key);                          sb.append( entry.getKey() );
82                          sb.append("=");                          sb.append("=");
83                          sb.append( encode(val) );                          sb.append( encode( entry.getValue() ) );
84                                                    
85                                                    
86                  }                  }
# Line 114  public class HttpUtil { Line 115  public class HttpUtil {
115                  URLConnection connection = url.openConnection();                  URLConnection connection = url.openConnection();
116                                    
117                  connection.setConnectTimeout(timeout);                  connection.setConnectTimeout(timeout);
118                    connection.setReadTimeout(timeout);
119                  InputStream is = connection.getInputStream();                  InputStream is = connection.getInputStream();
120                                    
121                  return readInputStream(is);                  return readInputStream(is);
# Line 127  public class HttpUtil { Line 129  public class HttpUtil {
129                  connection.setConnectTimeout(timeout);                  connection.setConnectTimeout(timeout);
130                                    
131                  OutputStream os = connection.getOutputStream();                  OutputStream os = connection.getOutputStream();
132                  os.write( data.getBytes() );                  os.write( data.getBytes(Charset.forName("UTF-8")) );
133                                    
134                  InputStream is = connection.getInputStream();                  InputStream is = connection.getInputStream();
135                                    
# Line 137  public class HttpUtil { Line 139  public class HttpUtil {
139                    
140          static byte[] readInputStream(InputStream is) throws IOException{          static byte[] readInputStream(InputStream is) throws IOException{
141                  byte buffer[] = new byte[1024];                  byte buffer[] = new byte[1024];
142                  ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes                  //try /; //start buffer size - instead of default 32bytes
143                  try {                  try (ByteArrayOutputStream baos = new ByteArrayOutputStream(32768)) {
144                          int count;                          int count;
145                            
146                          while ( (count = is.read(buffer)) != -1) {                          while ( (count = is.read(buffer)) != -1) {
147                                  baos.write(buffer, 0, count);                                  baos.write(buffer, 0, count);
148                          }                          }
149                  } finally {                          is.close();
150                          try {                          return baos.toByteArray();
151                                  is.close();                  }      
                                 baos.close();  
                         } catch (Exception e) {} //never mind if close throws an exception  
                 }  
                                   
                 return baos.toByteArray();                
152          }          }
153                    
154  }  }

Legend:
Removed from v.2321  
changed lines
  Added in v.2618

  ViewVC Help
Powered by ViewVC 1.1.20