11
11
import java .io .OutputStream ;
12
12
import java .net .HttpURLConnection ;
13
13
import java .net .MalformedURLException ;
14
+ import java .net .ProtocolException ;
14
15
import java .net .URL ;
15
16
import java .util .ArrayList ;
16
17
import java .util .HashMap ;
@@ -22,6 +23,7 @@ class LuaHTTPS {
22
23
static private String TAG = "LuaHTTPS" ;
23
24
24
25
private String urlString ;
26
+ private String method ;
25
27
private byte [] postData ;
26
28
private byte [] response ;
27
29
private int responseCode ;
@@ -34,6 +36,7 @@ public LuaHTTPS() {
34
36
35
37
public void reset () {
36
38
urlString = null ;
39
+ method = "GET" ;
37
40
postData = null ;
38
41
response = null ;
39
42
responseCode = 0 ;
@@ -50,6 +53,11 @@ public void setPostData(byte[] postData) {
50
53
this .postData = postData ;
51
54
}
52
55
56
+ @ Keep
57
+ public void setMethod (String method ) {
58
+ this .method = method .toUpperCase ();
59
+ }
60
+
53
61
@ Keep
54
62
public void addHeader (String key , String value ) {
55
63
headers .put (key , value );
@@ -110,13 +118,21 @@ public boolean request() {
110
118
return false ;
111
119
}
112
120
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
+
113
129
// Set header
114
130
for (Map .Entry <String , String > headerData : headers .entrySet ()) {
115
131
connection .setRequestProperty (headerData .getKey (), headerData .getValue ());
116
132
}
117
133
118
134
// Set post data
119
- if (postData != null ) {
135
+ if (postData != null && canSendData () ) {
120
136
connection .setDoOutput (true );
121
137
connection .setChunkedStreamingMode (0 );
122
138
@@ -168,4 +184,8 @@ public boolean request() {
168
184
connection .disconnect ();
169
185
return true ;
170
186
}
187
+
188
+ private boolean canSendData () {
189
+ return !method .equals ("GET" ) && !method .equals ("HEAD" );
190
+ }
171
191
}
0 commit comments