Skip to content

Commit f4e9512

Browse files
committed
添加默认的Https设置,post请求可以传空参数
1 parent 0b23a6c commit f4e9512

File tree

4 files changed

+156
-56
lines changed

4 files changed

+156
-56
lines changed

KJFrame/app/src/main/java/org/kymjs/blog/ui/Login.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
*/
1616
package org.kymjs.blog.ui;
1717

18+
import android.text.Editable;
19+
import android.text.TextWatcher;
20+
import android.view.View;
21+
import android.widget.Button;
22+
import android.widget.EditText;
23+
import android.widget.ImageView;
24+
import android.widget.RelativeLayout;
25+
1826
import org.kymjs.blog.R;
1927
import org.kymjs.blog.domain.LoginData;
2028
import org.kymjs.blog.domain.User;
@@ -33,14 +41,6 @@
3341
import org.kymjs.kjframe.utils.StringUtils;
3442
import org.kymjs.kjframe.widget.RoundImageView;
3543

36-
import android.text.Editable;
37-
import android.text.TextWatcher;
38-
import android.view.View;
39-
import android.widget.Button;
40-
import android.widget.EditText;
41-
import android.widget.ImageView;
42-
import android.widget.RelativeLayout;
43-
4444
/**
4545
* 新用户登陆界面
4646
*
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2015, 张涛.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.kymjs.kjframe.http;
18+
19+
import java.security.KeyManagementException;
20+
import java.security.NoSuchAlgorithmException;
21+
import java.security.SecureRandom;
22+
import java.security.cert.X509Certificate;
23+
24+
import javax.net.ssl.HostnameVerifier;
25+
import javax.net.ssl.HttpsURLConnection;
26+
import javax.net.ssl.SSLContext;
27+
import javax.net.ssl.SSLSession;
28+
import javax.net.ssl.TrustManager;
29+
import javax.net.ssl.X509TrustManager;
30+
31+
/**
32+
* @author kymjs (http://www.kymjs.com/) on 9/23/15.
33+
*/
34+
public class HTTPSTrustManager implements X509TrustManager {
35+
36+
private static TrustManager[] trustManagers;
37+
private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[]{};
38+
39+
@Override
40+
public void checkClientTrusted(
41+
java.security.cert.X509Certificate[] x509Certificates, String s)
42+
throws java.security.cert.CertificateException {
43+
// To change body of implemented methods use File | Settings | File
44+
// Templates.
45+
}
46+
47+
@Override
48+
public void checkServerTrusted(
49+
java.security.cert.X509Certificate[] x509Certificates, String s)
50+
throws java.security.cert.CertificateException {
51+
// To change body of implemented methods use File | Settings | File
52+
// Templates.
53+
}
54+
55+
public boolean isClientTrusted(X509Certificate[] chain) {
56+
return true;
57+
}
58+
59+
public boolean isServerTrusted(X509Certificate[] chain) {
60+
return true;
61+
}
62+
63+
@Override
64+
public X509Certificate[] getAcceptedIssuers() {
65+
return _AcceptedIssuers;
66+
}
67+
68+
public static void allowAllSSL() {
69+
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
70+
@Override
71+
public boolean verify(String arg0, SSLSession arg1) {
72+
return true;
73+
}
74+
});
75+
76+
SSLContext context = null;
77+
if (trustManagers == null) {
78+
trustManagers = new TrustManager[]{new HTTPSTrustManager()};
79+
}
80+
81+
try {
82+
context = SSLContext.getInstance("TLS");
83+
context.init(null, trustManagers, new SecureRandom());
84+
} catch (NoSuchAlgorithmException e) {
85+
e.printStackTrace();
86+
} catch (KeyManagementException e) {
87+
e.printStackTrace();
88+
}
89+
HttpsURLConnection.setDefaultSSLSocketFactory(context
90+
.getSocketFactory());
91+
}
92+
93+
}

KJFrame/kjframe/src/main/java/org/kymjs/kjframe/http/HttpConnectStack.java

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616

1717
package org.kymjs.kjframe.http;
1818

19+
import org.apache.http.Header;
20+
import org.apache.http.HttpEntity;
21+
import org.apache.http.HttpResponse;
22+
import org.apache.http.ProtocolVersion;
23+
import org.apache.http.StatusLine;
24+
import org.apache.http.entity.BasicHttpEntity;
25+
import org.apache.http.message.BasicHeader;
26+
import org.apache.http.message.BasicHttpResponse;
27+
import org.apache.http.message.BasicStatusLine;
28+
import org.kymjs.kjframe.http.Request.HttpMethod;
29+
1930
import java.io.DataOutputStream;
2031
import java.io.IOException;
2132
import java.io.InputStream;
@@ -29,17 +40,6 @@
2940
import javax.net.ssl.HttpsURLConnection;
3041
import javax.net.ssl.SSLSocketFactory;
3142

32-
import org.apache.http.Header;
33-
import org.apache.http.HttpEntity;
34-
import org.apache.http.HttpResponse;
35-
import org.apache.http.ProtocolVersion;
36-
import org.apache.http.StatusLine;
37-
import org.apache.http.entity.BasicHttpEntity;
38-
import org.apache.http.message.BasicHeader;
39-
import org.apache.http.message.BasicHttpResponse;
40-
import org.apache.http.message.BasicStatusLine;
41-
import org.kymjs.kjframe.http.Request.HttpMethod;
42-
4343
/**
4444
* HttpUrlConnection方式实现
4545
*/
@@ -66,14 +66,14 @@ public HttpConnectStack(UrlRewriter urlRewriter) {
6666
}
6767

6868
public HttpConnectStack(UrlRewriter urlRewriter,
69-
SSLSocketFactory sslSocketFactory) {
69+
SSLSocketFactory sslSocketFactory) {
7070
mUrlRewriter = urlRewriter;
7171
mSslSocketFactory = sslSocketFactory;
7272
}
7373

7474
@Override
7575
public HttpResponse performRequest(Request<?> request,
76-
Map<String, String> additionalHeaders) throws IOException {
76+
Map<String, String> additionalHeaders) throws IOException {
7777
String url = request.getUrl();
7878
HashMap<String, String> map = new HashMap<String, String>();
7979
map.putAll(request.getHeaders());
@@ -147,55 +147,61 @@ private HttpURLConnection openConnection(URL url, Request<?> request)
147147
connection.setDoInput(true);
148148

149149
// use caller-provided custom SslSocketFactory, if any, for HTTPS
150-
if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
151-
((HttpsURLConnection) connection)
152-
.setSSLSocketFactory(mSslSocketFactory);
150+
if ("https".equals(url.getProtocol())) {
151+
if (mSslSocketFactory != null) {
152+
((HttpsURLConnection) connection)
153+
.setSSLSocketFactory(mSslSocketFactory);
154+
} else {
155+
//信任所有证书
156+
HTTPSTrustManager.allowAllSSL();
157+
}
153158
}
154159

155160
return connection;
156161
}
157162

158-
/* package */static void setConnectionParametersForRequest(
163+
/* package */
164+
static void setConnectionParametersForRequest(
159165
HttpURLConnection connection, Request<?> request)
160166
throws IOException {
161167
switch (request.getMethod()) {
162-
case HttpMethod.GET:
163-
connection.setRequestMethod("GET");
164-
break;
165-
case HttpMethod.DELETE:
166-
connection.setRequestMethod("DELETE");
167-
break;
168-
case HttpMethod.POST:
169-
connection.setRequestMethod("POST");
170-
addBodyIfExists(connection, request);
171-
break;
172-
case HttpMethod.PUT:
173-
connection.setRequestMethod("PUT");
174-
addBodyIfExists(connection, request);
175-
break;
176-
case HttpMethod.HEAD:
177-
connection.setRequestMethod("HEAD");
178-
break;
179-
case HttpMethod.OPTIONS:
180-
connection.setRequestMethod("OPTIONS");
181-
break;
182-
case HttpMethod.TRACE:
183-
connection.setRequestMethod("TRACE");
184-
break;
185-
case HttpMethod.PATCH:
186-
connection.setRequestMethod("PATCH");
187-
addBodyIfExists(connection, request);
188-
break;
189-
default:
190-
throw new IllegalStateException("Unknown method type.");
168+
case HttpMethod.GET:
169+
connection.setRequestMethod("GET");
170+
break;
171+
case HttpMethod.DELETE:
172+
connection.setRequestMethod("DELETE");
173+
break;
174+
case HttpMethod.POST:
175+
connection.setRequestMethod("POST");
176+
addBodyIfExists(connection, request);
177+
break;
178+
case HttpMethod.PUT:
179+
connection.setRequestMethod("PUT");
180+
addBodyIfExists(connection, request);
181+
break;
182+
case HttpMethod.HEAD:
183+
connection.setRequestMethod("HEAD");
184+
break;
185+
case HttpMethod.OPTIONS:
186+
connection.setRequestMethod("OPTIONS");
187+
break;
188+
case HttpMethod.TRACE:
189+
connection.setRequestMethod("TRACE");
190+
break;
191+
case HttpMethod.PATCH:
192+
connection.setRequestMethod("PATCH");
193+
addBodyIfExists(connection, request);
194+
break;
195+
default:
196+
throw new IllegalStateException("Unknown method type.");
191197
}
192198
}
193199

194200
/**
195201
* 如果有body则添加
196202
*/
197203
private static void addBodyIfExists(HttpURLConnection connection,
198-
Request<?> request) throws IOException {
204+
Request<?> request) throws IOException {
199205
byte[] body = request.getBody();
200206
if (body != null) {
201207
connection.setDoOutput(true);

KJFrame/kjframe/src/main/java/org/kymjs/kjframe/http/HttpParams.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.http.HttpEntity;
2424
import org.apache.http.message.BasicHeader;
2525
import org.kymjs.kjframe.utils.FileUtils;
26+
import org.kymjs.kjframe.utils.StringUtils;
2627

2728
import java.io.ByteArrayInputStream;
2829
import java.io.ByteArrayOutputStream;
@@ -227,7 +228,7 @@ public void writeTo(final OutputStream outstream) throws IOException {
227228
mOutputStream.write(endString.getBytes());
228229
//
229230
outstream.write(mOutputStream.toByteArray());
230-
} else {
231+
} else if (!StringUtils.isEmpty(getUrlParams())) {
231232
outstream.write(getUrlParams().substring(1).getBytes());
232233
}
233234
}

0 commit comments

Comments
 (0)