HttpURLConnectionBuilder.java
5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package net.hockeyapp.android.utils;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.Build.VERSION;
import android.text.TextUtils;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class HttpURLConnectionBuilder
{
public static final String DEFAULT_CHARSET = "UTF-8";
private final String a;
private String b;
private String c;
private SimpleMultipartEntity d;
private int e = 120000;
private final Map<String, String> f;
public HttpURLConnectionBuilder(String paramString)
{
this.a = paramString;
this.f = new HashMap();
}
public HttpURLConnection build()
throws IOException
{
HttpURLConnection localHttpURLConnection = (HttpURLConnection)new URL(this.a).openConnection();
localHttpURLConnection.setConnectTimeout(this.e);
localHttpURLConnection.setReadTimeout(this.e);
if (Build.VERSION.SDK_INT <= 9) {
localHttpURLConnection.setRequestProperty("Connection", "close");
}
if (!TextUtils.isEmpty(this.b))
{
localHttpURLConnection.setRequestMethod(this.b);
if ((!TextUtils.isEmpty(this.c)) || (this.b.equalsIgnoreCase("POST")) || (this.b.equalsIgnoreCase("PUT"))) {
localHttpURLConnection.setDoOutput(true);
}
}
Object localObject = this.f.keySet().iterator();
while (((Iterator)localObject).hasNext())
{
String str = (String)((Iterator)localObject).next();
localHttpURLConnection.setRequestProperty(str, (String)this.f.get(str));
}
if (!TextUtils.isEmpty(this.c))
{
localObject = new BufferedWriter(new OutputStreamWriter(localHttpURLConnection.getOutputStream(), "UTF-8"));
((BufferedWriter)localObject).write(this.c);
((BufferedWriter)localObject).flush();
((BufferedWriter)localObject).close();
}
if (this.d != null)
{
localHttpURLConnection.setRequestProperty("Content-Length", String.valueOf(this.d.getContentLength()));
localObject = new BufferedOutputStream(localHttpURLConnection.getOutputStream());
((BufferedOutputStream)localObject).write(this.d.getOutputStream().toByteArray());
((BufferedOutputStream)localObject).flush();
((BufferedOutputStream)localObject).close();
}
return localHttpURLConnection;
}
public HttpURLConnectionBuilder setBasicAuthorization(String paramString1, String paramString2)
{
setHeader("Authorization", "Basic " + Base64.encodeToString(new StringBuilder().append(paramString1).append(":").append(paramString2).toString().getBytes(), 2));
return this;
}
public HttpURLConnectionBuilder setHeader(String paramString1, String paramString2)
{
this.f.put(paramString1, paramString2);
return this;
}
public HttpURLConnectionBuilder setRequestBody(String paramString)
{
this.c = paramString;
return this;
}
public HttpURLConnectionBuilder setRequestMethod(String paramString)
{
this.b = paramString;
return this;
}
public HttpURLConnectionBuilder setTimeout(int paramInt)
{
if (paramInt < 0) {
throw new IllegalArgumentException("Timeout has to be positive.");
}
this.e = paramInt;
return this;
}
public HttpURLConnectionBuilder writeFormFields(Map<String, String> paramMap)
{
try
{
ArrayList localArrayList = new ArrayList();
Iterator localIterator = paramMap.keySet().iterator();
while (localIterator.hasNext())
{
String str2 = (String)localIterator.next();
String str1 = (String)paramMap.get(str2);
str2 = URLEncoder.encode(str2, "UTF-8");
str1 = URLEncoder.encode(str1, "UTF-8");
localArrayList.add(str2 + "=" + str1);
}
paramMap = TextUtils.join("&", localArrayList);
}
catch (UnsupportedEncodingException paramMap)
{
throw new RuntimeException(paramMap);
}
setHeader("Content-Type", "application/x-www-form-urlencoded");
setRequestBody(paramMap);
return this;
}
public HttpURLConnectionBuilder writeMultipartData(Map<String, String> paramMap, Context paramContext, List<Uri> paramList)
{
Object localObject;
try
{
this.d = new SimpleMultipartEntity();
this.d.writeFirstBoundaryIfNeeds();
localObject = paramMap.keySet().iterator();
while (((Iterator)localObject).hasNext())
{
String str = (String)((Iterator)localObject).next();
this.d.addPart(str, (String)paramMap.get(str));
}
i = 0;
}
catch (IOException paramMap)
{
throw new RuntimeException(paramMap);
}
int i;
if (i < paramList.size())
{
localObject = (Uri)paramList.get(i);
if (i != paramList.size() - 1) {
break label225;
}
}
label225:
for (boolean bool = true;; bool = false)
{
paramMap = paramContext.getContentResolver().openInputStream((Uri)localObject);
localObject = ((Uri)localObject).getLastPathSegment();
this.d.addPart("attachment" + i, (String)localObject, paramMap, bool);
i += 1;
break;
this.d.writeLastBoundaryIfNeeds();
setHeader("Content-Type", "multipart/form-data; boundary=" + this.d.getBoundary());
return this;
}
}
}
/* Location: /home/merong/decompile/hackery-dex2jar.jar!/net/hockeyapp/android/utils/HttpURLConnectionBuilder.class
* Java compiler version: 6 (50.0)
* JD-Core Version: 0.7.1
*/