Changeset 32941 in osm
- Timestamp:
- 2016-09-07T23:37:53+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/action/OpenLinkAction.java
r30717 r32941 19 19 20 20 import java.awt.event.ActionEvent; 21 import java.io.File OutputStream;21 import java.io.File; 22 22 import java.io.IOException; 23 23 import java.io.InputStream; 24 import java.io.OutputStreamWriter;25 import java.net.HttpURLConnection;26 import java.net.MalformedURLException;27 24 import java.net.URL; 25 import java.nio.charset.StandardCharsets; 26 import java.nio.file.Files; 28 27 29 28 import org.openstreetmap.josm.Main; … … 32 31 import org.openstreetmap.josm.plugins.tag2link.data.Link; 33 32 import org.openstreetmap.josm.plugins.tag2link.data.LinkPost; 33 import org.openstreetmap.josm.tools.HttpClient; 34 34 import org.openstreetmap.josm.tools.OpenBrowser; 35 35 … … 37 37 * Action allowing to open a general link. 38 38 * @author Don-vip 39 *40 39 */ 41 40 @SuppressWarnings("serial") … … 58 57 try { 59 58 LinkPost lp = (LinkPost) link; 60 System.out.println("Sending POST request to "+lp.url);61 HttpURLConnection conn = (HttpURLConnection) new URL(lp.url).openConnection();62 conn.setDoOutput(true);63 conn.setRequestMethod("POST");64 for (String header : lp.headers.keySet()) {65 conn.setRequestProperty(header, lp.headers.get(header));66 }67 59 String data = ""; 68 60 for (String param : lp.params.keySet()) { … … 72 64 data += param+"="+lp.params.get(param); 73 65 } 74 try (OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream())) {75 osw.write(data);76 osw.flush();77 }78 66 79 67 String filename = "output.pdf";// FIXME: should work for PDF files only (not even tested) 80 try (FileOutputStream fos = new FileOutputStream(filename); 81 InputStream is = conn.getInputStream()) { 82 byte[] buffer = new byte[2048]; 83 int n = -1; 84 while ((n = is.read(buffer)) > -1) { 85 fos.write(buffer, 0, n); 86 } 68 try (InputStream is = HttpClient.create(new URL(lp.url), "POST").setHeaders(lp.headers) 69 .setRequestBody(data.getBytes(StandardCharsets.UTF_8)).connect().getContent()) { 70 Files.copy(is, new File(filename).toPath()); 87 71 } 88 72 … … 92 76 Main.error(result); 93 77 } 94 95 } catch (MalformedURLException ex) {96 Main.error(ex);97 78 } catch (IOException ex) { 98 79 Main.error(ex);
Note:
See TracChangeset
for help on using the changeset viewer.