1111import java .io .OutputStream ;
1212import java .net .HttpURLConnection ;
1313import java .net .MalformedURLException ;
14+ import java .net .ProtocolException ;
1415import java .net .URL ;
1516import java .util .ArrayList ;
1617import java .util .HashMap ;
@@ -22,6 +23,7 @@ class LuaHTTPS {
2223 static private String TAG = "LuaHTTPS" ;
2324
2425 private String urlString ;
26+ private String method ;
2527 private byte [] postData ;
2628 private byte [] response ;
2729 private int responseCode ;
@@ -34,6 +36,7 @@ public LuaHTTPS() {
3436
3537 public void reset () {
3638 urlString = null ;
39+ method = "GET" ;
3740 postData = null ;
3841 response = null ;
3942 responseCode = 0 ;
@@ -50,6 +53,11 @@ public void setPostData(byte[] postData) {
5053 this .postData = postData ;
5154 }
5255
56+ @ Keep
57+ public void setMethod (String method ) {
58+ this .method = method .toUpperCase ();
59+ }
60+
5361 @ Keep
5462 public void addHeader (String key , String value ) {
5563 headers .put (key , value );
@@ -110,13 +118,21 @@ public boolean request() {
110118 return false ;
111119 }
112120
121+ // Set request method
122+ try {
123+ connection .setRequestMethod (method );
124+ } catch (ProtocolException e ) {
125+ Log .e (TAG , "Error" , e );
126+ return false ;
127+ }
128+
113129 // Set header
114130 for (Map .Entry <String , String > headerData : headers .entrySet ()) {
115131 connection .setRequestProperty (headerData .getKey (), headerData .getValue ());
116132 }
117133
118134 // Set post data
119- if (postData != null ) {
135+ if (postData != null && canSendData () ) {
120136 connection .setDoOutput (true );
121137 connection .setChunkedStreamingMode (0 );
122138
@@ -168,4 +184,8 @@ public boolean request() {
168184 connection .disconnect ();
169185 return true ;
170186 }
187+
188+ private boolean canSendData () {
189+ return !method .equals ("GET" ) && !method .equals ("HEAD" );
190+ }
171191}
0 commit comments