Mercurial Hosting > luan
comparison src/luan/modules/url/LuanUrl.java @ 1363:1a7b8e38921a
add url post content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 29 May 2019 15:11:51 -0600 |
parents | 2449ca95dc1e |
children | 45363886f256 |
comparison
equal
deleted
inserted
replaced
1362:77f2d091f17f | 1363:1a7b8e38921a |
---|---|
31 private static enum Method { GET, POST, DELETE } | 31 private static enum Method { GET, POST, DELETE } |
32 | 32 |
33 public final URL url; | 33 public final URL url; |
34 private Method method = Method.GET; | 34 private Method method = Method.GET; |
35 private final Map<String,Object> headers = new HashMap<String,Object>(); | 35 private final Map<String,Object> headers = new HashMap<String,Object>(); |
36 private String content = ""; | 36 private static final byte[] NO_CONTENT = new byte[0]; |
37 private byte[] content = NO_CONTENT; | |
37 private MultipartClient multipart = null; | 38 private MultipartClient multipart = null; |
38 private int timeout = 0; | 39 private int timeout = 0; |
39 private String authUsername = null; | 40 private String authUsername = null; |
40 private String authPassword = null; | 41 private String authPassword = null; |
41 | 42 |
90 authPassword = password; | 91 authPassword = password; |
91 } | 92 } |
92 } | 93 } |
93 Map params = getMap(map,"parameters"); | 94 Map params = getMap(map,"parameters"); |
94 String enctype = getString(map,"enctype"); | 95 String enctype = getString(map,"enctype"); |
96 Object content = map.remove("content"); | |
97 if( content != null ) { | |
98 if( this.method != Method.POST ) | |
99 throw new LuanException( "content can only be used with POST" ); | |
100 if( params != null ) | |
101 throw new LuanException( "content cannot be used with parameters" ); | |
102 if( content instanceof String ) { | |
103 this.content = ((String)content).getBytes(); | |
104 } else if( content instanceof byte[] ) { | |
105 this.content = (byte[])content; | |
106 } else | |
107 throw new LuanException( "content must be String or byte[]" ); | |
108 } | |
95 if( enctype != null ) { | 109 if( enctype != null ) { |
96 if( !enctype.equals("multipart/form-data") ) | 110 if( !enctype.equals("multipart/form-data") ) |
97 throw new LuanException( "unrecognized enctype: "+enctype ); | 111 throw new LuanException( "unrecognized enctype: "+enctype ); |
98 if( this.method!=Method.POST ) | 112 if( this.method != Method.POST ) |
99 throw new LuanException( "multipart/form-data can only be used with POST" ); | 113 throw new LuanException( "multipart/form-data can only be used with POST" ); |
100 if( params==null ) | 114 if( params==null ) |
101 throw new LuanException( "multipart/form-data requires parameters" ); | 115 throw new LuanException( "multipart/form-data requires parameters" ); |
102 if( params.isEmpty() ) | 116 if( params.isEmpty() ) |
103 throw new LuanException( "multipart/form-data parameters can't be empty" ); | 117 throw new LuanException( "multipart/form-data parameters can't be empty" ); |
126 sb.append( keyEnc ).append( '=' ).append( encode((String)obj) ); | 140 sb.append( keyEnc ).append( '=' ).append( encode((String)obj) ); |
127 } | 141 } |
128 } | 142 } |
129 } | 143 } |
130 if( this.method==Method.POST ) { | 144 if( this.method==Method.POST ) { |
131 content = sb.toString(); | 145 this.content = sb.toString().getBytes(); |
132 } else { | 146 } else { |
133 String urlS = url.toString(); | 147 String urlS = url.toString(); |
134 if( urlS.indexOf('?') == -1 ) { | 148 if( urlS.indexOf('?') == -1 ) { |
135 urlS += '?'; | 149 urlS += '?'; |
136 } else { | 150 } else { |
254 | 268 |
255 OutputStream out; | 269 OutputStream out; |
256 if( multipart != null ) { | 270 if( multipart != null ) { |
257 out = multipart.write(httpCon); | 271 out = multipart.write(httpCon); |
258 } else { | 272 } else { |
259 byte[] post = content.getBytes(); | 273 // httpCon.setRequestProperty("Content-Length",Integer.toString(content.length)); |
260 // httpCon.setRequestProperty("Content-Length",Integer.toString(post.length)); | |
261 out = httpCon.getOutputStream(); | 274 out = httpCon.getOutputStream(); |
262 out.write(post); | 275 out.write(content); |
263 } | 276 } |
264 out.flush(); | 277 out.flush(); |
265 try { | 278 try { |
266 return getInputStream(httpCon,authorization); | 279 return getInputStream(httpCon,authorization); |
267 } finally { | 280 } finally { |