mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-07-26 00:00:14 +08:00
Now working for both ios and android with same api. Updated AFNetworking
This commit is contained in:
@@ -25,6 +25,8 @@ import javax.net.ssl.HostnameVerifier;
|
||||
import java.util.Iterator;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
|
||||
public abstract class CordovaHttp {
|
||||
protected static final String TAG = "CordovaHTTP";
|
||||
@@ -89,12 +91,15 @@ public abstract class CordovaHttp {
|
||||
return this.callbackContext;
|
||||
}
|
||||
|
||||
protected boolean sslPinning() {
|
||||
return sslPinning;
|
||||
}
|
||||
|
||||
protected boolean acceptAllCerts() {
|
||||
return acceptAllCerts;
|
||||
protected HttpRequest setupSecurity(HttpRequest request) {
|
||||
if (acceptAllCerts) {
|
||||
request.trustAllCerts();
|
||||
request.trustAllHosts();
|
||||
}
|
||||
if (sslPinning) {
|
||||
request.pinToCerts();
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
protected void respondWithError(String msg) {
|
||||
|
||||
@@ -31,13 +31,7 @@ public class CordovaHttpDownload extends CordovaHttp implements Runnable {
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), true);
|
||||
if (this.acceptAllCerts()) {
|
||||
request.trustAllCerts();
|
||||
request.trustAllHosts();
|
||||
}
|
||||
if (this.sslPinning()) {
|
||||
request.pinToCerts();
|
||||
}
|
||||
this.setupSecurity(request);
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
int code = request.code();
|
||||
|
||||
@@ -31,21 +31,11 @@ public class CordovaHttpGet extends CordovaHttp implements Runnable {
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), true);
|
||||
if (this.acceptAllCerts()) {
|
||||
request.trustAllCerts();
|
||||
request.trustAllHosts();
|
||||
}
|
||||
if (this.sslPinning()) {
|
||||
request.pinToCerts();
|
||||
}
|
||||
this.setupSecurity(request);
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
int code = request.code();
|
||||
String body = request.body(CHARSET);
|
||||
|
||||
Log.d(TAG, Integer.toString(code));
|
||||
Log.d(TAG, body);
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", code);
|
||||
if (code >= 200 && code < 300) {
|
||||
|
||||
@@ -22,27 +22,13 @@ public class CordovaHttpPost extends CordovaHttp implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Log.d(TAG, this.getParams().toString());
|
||||
HttpRequest request = HttpRequest.post(this.getUrlString());
|
||||
if (this.acceptAllCerts()) {
|
||||
request.trustAllCerts();
|
||||
request.trustAllHosts();
|
||||
}
|
||||
if (this.sslPinning()) {
|
||||
Log.d(TAG, "ssl pinning");
|
||||
request.pinToCerts();
|
||||
}
|
||||
this.setupSecurity(request);
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
|
||||
request.form(this.getParams());
|
||||
|
||||
int code = request.code();
|
||||
String body = request.body(CHARSET);
|
||||
|
||||
Log.d(TAG, Integer.toString(code));
|
||||
Log.d(TAG, body);
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", code);
|
||||
if (code >= 200 && code < 300) {
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
|
||||
@@ -34,21 +35,17 @@ public class CordovaHttpUpload extends CordovaHttp implements Runnable {
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.post(this.getUrlString());
|
||||
if (this.acceptAllCerts()) {
|
||||
request.trustAllCerts();
|
||||
request.trustAllHosts();
|
||||
}
|
||||
if (this.sslPinning()) {
|
||||
request.pinToCerts();
|
||||
}
|
||||
this.setupSecurity(request);
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
URI uri = new URI(filePath);
|
||||
Log.d(TAG, uri.toString());
|
||||
Log.d(TAG, name);
|
||||
int index = filePath.lastIndexOf('/');
|
||||
String filename = filePath.substring(index);
|
||||
request.part(this.name, filename, "image/jpeg", new File(uri));
|
||||
String filename = filePath.substring(index + 1);
|
||||
index = filePath.lastIndexOf('.');
|
||||
String ext = filePath.substring(index + 1);
|
||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||
String mimeType = mimeTypeMap.getMimeTypeFromExtension(ext);
|
||||
request.part(this.name, filename, mimeType, new File(uri));
|
||||
|
||||
Set<?> set = (Set<?>)this.getParams().entrySet();
|
||||
Iterator<?> i = set.iterator();
|
||||
|
||||
Reference in New Issue
Block a user