POST请求错误内容
java.io.FileNotFoundException: http://www.xxxxx.net:8080/test/test/
at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
at org.cocos2dx.lib.QuickHTTPInterface.close(QuickHTTPInterface.java:489)
at org.cocos2dx.lib.Cocos2dxRenderer.nativeRender(Native Method)
at org.cocos2dx.lib.Cocos2dxRenderer.onDrawFrame(Cocos2dxRenderer.java:104)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
并且在http.getResponseCode()错误码为403,很奇怪为什么会这样。因为用curl是正常的。
问题点:
HttpURLConnection重定向问题,在curl因为用的自动处理没太在意,然而HttpURLConnection好像自动处理不正常。没办法只能手动处理了。
http.setInstanceFollowRedirects(false); //不自动处理重定向
设置当前对象手动处理重定向。这样 http.getResponseCode()可能出现 301 和 302然后手动处理重定向问题
static HttpURLConnection reloadRedirectHttpURLConnection(HttpURLConnection http) {
HttpURLConnection redirectHttp = http;
try {
int statusCode = http.getResponseCode();
// https redirect
if (statusCode == HttpURLConnection.HTTP_MOVED_TEMP || statusCode == HttpURLConnection.HTTP_MOVED_PERM || statusCode == HttpURLConnection.HTTP_SEE_OTHER) {
String newUrl = http.getHeaderField("Location");
String cookies = http.getHeaderField("Set-Cookie");
redirectHttp = (HttpURLConnection) new URL(newUrl).openConnection();
redirectHttp.setRequestProperty("Cookie", cookies);
Log.i("QuickHTTPInterface", "Redirect to URL : " + newUrl);
redirectHttp = reloadRedirectHttpURLConnection(redirectHttp);
}
} catch (IOException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
return redirectHttp;
}
写的一个递归先暂时这样解决一下问题。
贴上修改好的 QuickHTTPInterface 代码 目前处理得不是很好。不改 C++代码
package org.cocos2dx.lib;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class QuickHTTPInterface {
static String BOUNDARY = "----------------------------78631b43218d";
static String NEWLINE = "\r\n";
static HashMap<HttpURLConnection, HttpURLConnection> redirects = new HashMap<HttpURLConnection, HttpURLConnection>();
static HttpURLConnection createURLConnect(String strURL) {
URL url;
HttpURLConnection urlConnection;
try {
url = new URL(strURL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Accept-Encoding", "identity");
urlConnection.setDoInput(true);
} catch (Exception e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
return null;
}
return urlConnection;
}
static void setRequestMethod(HttpURLConnection http, String strMedthod) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
try {
if ("POST".equalsIgnoreCase(strMedthod)) {
http.setDoOutput(true);
http.setUseCaches(false);
http.setInstanceFollowRedirects(false); //不自动处理重定向
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
}
http.setRequestMethod(strMedthod);
} catch (ProtocolException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
}
static void addRequestHeader(HttpURLConnection http, String strkey, String strValue, boolean bNeedBoundary) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
if ("Content-Type".equalsIgnoreCase(strkey.trim()) && bNeedBoundary) {
strValue += ("; boundary=" + BOUNDARY);
}
http.setRequestProperty(strkey, strValue);
}
static void setTimeout(HttpURLConnection http, int msTime) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
http.setConnectTimeout(msTime);
http.setReadTimeout(msTime);
}
static int connect(HttpURLConnection http) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
int nSuc = 0;
try {
http.connect();
} catch (IOException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
nSuc = 1;
}
return nSuc;
}
static void postContent(HttpURLConnection http, String name, String value, boolean bNeedConnectSym) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
try {
DataOutputStream out = new DataOutputStream(http.getOutputStream());
String content = null;
if (null == name || 0 == name.length()) {
content = java.net.URLEncoder.encode(value, "utf-8");
} else {
content = java.net.URLEncoder.encode(name, "utf-8") + "=" + java.net.URLEncoder.encode(value, "utf-8");
}
if (bNeedConnectSym) {
content = "&" + content;
}
out.write(content.getBytes());
out.flush();
} catch (IOException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
}
static void postContentByteArray(HttpURLConnection http, byte[] byteArray) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
try {
OutputStream out = http.getOutputStream();
out.write(byteArray);
out.flush();
} catch (IOException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
}
static void postFormContent(HttpURLConnection http, String key, String val) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
try {
OutputStream out = http.getOutputStream();
out.write(getBoundaryContentHeader(key, val).getBytes());
out.flush();
} catch (IOException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
}
static void postFormFile(HttpURLConnection http, String name, String filePath) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
try {
FileInputStream fin = new FileInputStream(filePath);
OutputStream out = http.getOutputStream();
out.write(getBoundaryFileHeader(name, filePath).getBytes());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fin.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.write(NEWLINE.getBytes());
out.flush();
fin.close();
} catch (IOException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
}
static void postFormEnd(HttpURLConnection http, boolean bBoundary) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
if ("GET".equalsIgnoreCase(http.getRequestMethod())) {
return;
}
try {
OutputStream out = http.getOutputStream();
if (bBoundary) {
out.write(getBoundaryEnd().getBytes());
out.flush();
}
out.flush();
out.close();
// https redirect
HttpURLConnection reloadHttp = reloadRedirectHttpURLConnection(http);
if (reloadHttp != null && reloadHttp != http) {
redirects.put(http, reloadHttp);
}
} catch (IOException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
}
private static HttpURLConnection reloadRedirectHttpURLConnection(HttpURLConnection http) {
HttpURLConnection redirectHttp = http;
try {
int statusCode = http.getResponseCode();
// https redirect
if (statusCode == HttpURLConnection.HTTP_MOVED_TEMP || statusCode == HttpURLConnection.HTTP_MOVED_PERM || statusCode == HttpURLConnection.HTTP_SEE_OTHER) {
String newUrl = http.getHeaderField("Location");
String cookies = http.getHeaderField("Set-Cookie");
redirectHttp = (HttpURLConnection) new URL(newUrl).openConnection();
redirectHttp.setRequestProperty("Cookie", cookies);
Log.i("QuickHTTPInterface", "Redirect to URL : " + newUrl);
redirectHttp = reloadRedirectHttpURLConnection(redirectHttp);
}
} catch (IOException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
return redirectHttp;
}
static String getBoundaryFileHeader(String key, String filePath) {
File file = new File(filePath);
StringBuilder sb = new StringBuilder();
sb.append("--");
sb.append(BOUNDARY);
sb.append(NEWLINE);
sb.append("Content-Disposition: form-data; ");
sb.append("name=\"");
sb.append(key);
sb.append("\"; ");
sb.append("filename=\"");
sb.append(file.getName());
sb.append("\"");
sb.append(NEWLINE);
sb.append("Content-Type: application/octet-stream");
sb.append(NEWLINE);
sb.append(NEWLINE);
return sb.toString();
}
static String getBoundaryContentHeader(String key, String val) {
StringBuilder sb = new StringBuilder();
sb.append("--");
sb.append(BOUNDARY);
sb.append(NEWLINE);
sb.append("Content-Disposition: form-data; name=\"");
sb.append(key);
sb.append("\"");
sb.append(NEWLINE);
sb.append(NEWLINE);
sb.append(val);
sb.append(NEWLINE);
return sb.toString();
}
static String getBoundaryEnd() {
StringBuilder sb = new StringBuilder();
sb.append("--");
sb.append(BOUNDARY);
sb.append("--");
sb.append(NEWLINE);
return sb.toString();
}
static int getResponedCode(HttpURLConnection http) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
int code = 0;
try {
code = http.getResponseCode();
// Log.i("QuickHTTPInterface", "reponed code:" + code);
} catch (IOException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
return code;
}
static String getResponedErr(HttpURLConnection http) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
String msg;
try {
msg = http.getResponseMessage();
} catch (IOException e) {
msg = e.toString();
Log.e("QuickHTTPInterface", msg);
}
return msg;
}
static String getResponedHeader(HttpURLConnection http) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
Map<String, List<String>> headers = http.getHeaderFields();
JSONObject json = new JSONObject();
try {
for (Entry<String, List<String>> entry : headers.entrySet()) {
String key = entry.getKey();
if (null == key) {
key = "";
}
List<String> value = entry.getValue();
JSONArray jsonArray = new JSONArray();
for (String strVal : value) {
jsonArray.put(strVal);
}
json.put(key, jsonArray);
}
} catch (JSONException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
return json.toString();
}
static String getResponedHeaderByIdx(HttpURLConnection http, int idx) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
Map<String, List<String>> headers = http.getHeaderFields();
if (null == headers) {
return null;
}
String header = null;
int counter = 0;
for (Entry<String, List<String>> entry : headers.entrySet()) {
if (counter == idx) {
String key = entry.getKey();
if (null == key) {
header = listToString(entry.getValue(), ",") + "\n";
} else {
header = key + ":" + listToString(entry.getValue(), ",") + "\n";
}
break;
}
counter++;
}
return header;
}
private static String parseResponedHeaderByKey(HttpURLConnection http, String key){
Map<String, List<String>> headers = http.getHeaderFields();
if (null == headers) {
return null;
}
String header = null;
for (Entry<String, List<String>> entry : headers.entrySet()) {
if (key.equalsIgnoreCase(entry.getKey())) {
if ("set-cookie".equalsIgnoreCase(key)) {
header = combinCookies(entry.getValue(), http.getURL().getHost());
} else {
header = listToString(entry.getValue(), ",");
}
break;
}
}
return header;
}
static String getResponedHeaderByKey(HttpURLConnection http, String key) {
if (null == key) {
return null;
}
String header = parseResponedHeaderByKey(http, key);
if (header == null || header.isEmpty()) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
header = parseResponedHeaderByKey(redirectHttp, key);
}
}
return header;
}
private static int parseResponedHeaderByKeyInt(HttpURLConnection http, String key){
String value = http.getHeaderField(key);
if (null == value) {
return 0;
} else {
return Integer.parseInt(value);
}
}
static int getResponedHeaderByKeyInt(HttpURLConnection http, String key) {
int value = parseResponedHeaderByKeyInt(http, key);
if (0 == value) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
return parseResponedHeaderByKeyInt(redirectHttp, key);
}
}
return value;
}
static int getContentLeng(HttpURLConnection http) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
return http.getContentLength();
}
static byte[] getResponedString(HttpURLConnection http) {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
http = redirectHttp;
}
InputStream in;
try {
in = http.getInputStream();
String contentEncoding = http.getContentEncoding();
if (contentEncoding != null) {
if (contentEncoding.equalsIgnoreCase("gzip")) {
in = new GZIPInputStream(http.getInputStream()); // reads 2 bytes to determine GZIP stream!
} else if (contentEncoding.equalsIgnoreCase("deflate")) {
in = new InflaterInputStream(http.getInputStream());
}
}
} catch (IOException e) {
in = http.getErrorStream();
} catch (Exception e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
return null;
}
try {
byte[] buffer = new byte[1024];
byte[] retBuf = null;
int len = in.read(buffer);
// Log.i("QuickHTTPInterface", "have recv data:" + len);
if (-1 == len) {
retBuf = new byte[1];
retBuf[0] = 0;
} else {
retBuf = new byte[len + 1];
retBuf[0] = 1;
System.arraycopy(buffer, 0, retBuf, 1, len);
}
return retBuf;
} catch (IOException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
return null;
}
static void close(HttpURLConnection http) {
try {
HttpURLConnection redirectHttp = redirects.get(http);
if (redirectHttp != null) {
redirects.remove(http);
redirectHttp.getInputStream().close();
redirectHttp.disconnect();
}
http.getInputStream().close();
} catch (IOException e) {
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
} finally {
if (http != null) {
http.disconnect();
}
}
}
public static String listToString(List<String> list, String strInterVal) {
if (list == null) {
return null;
}
StringBuilder result = new StringBuilder();
boolean flag = false;
for (String str : list) {
if (flag) {
result.append(strInterVal);
}
if (null == str) {
str = "";
}
result.append(str);
flag = true;
}
return result.toString();
}
private static void parseSession(String cookie_value) {
String gSession;
if (cookie_value == null)
return;
String[] cookies = cookie_value.split(";");
String cookie = null;
for (int i = 0; i < cookies.length; i++) {
cookie = cookies[i];
if (cookie.toLowerCase().startsWith("sessionid")) {
gSession = cookie;
Log.d("QuickHTTPInterface", "gSession:" + gSession);
break;
}
}
}
public static String combinCookies(List<String> list, String strDomain) {
StringBuilder sbCookies = new StringBuilder();
String strKey = null;
String strValue = null;
String strExpire = null;
boolean bSecure = false;
boolean bFirst = false;
for (String str : list) {
bSecure = false;
bFirst = true;
String[] parts = str.split(";");
for (String part : parts) {
String[] item = part.split("=");
if (bFirst) {
if (2 == item.length) {
strKey = item[0];
strValue = item[1];
} else {
strKey = "";
strValue = "";
}
bFirst = false;
}
if ("expires".equalsIgnoreCase(item[0].trim())) {
strExpire = str2Seconds(item[1].trim());
} else if ("secure".equalsIgnoreCase(item[0].trim())) {
bSecure = true;
} else if ("domain".equalsIgnoreCase(item[0].trim())) {
strDomain = item[1];
}
}
if (null == strDomain) {
strDomain = "none";
}
sbCookies.append(strDomain);
sbCookies.append('\t');
sbCookies.append("FALSE\t"); // access
sbCookies.append("/\t"); // path
if (bSecure) {
sbCookies.append("TRUE\t"); // secure
} else {
sbCookies.append("FALSE\t"); // secure
}
sbCookies.append(strExpire); // expire tag
sbCookies.append("\t");
sbCookies.append(strKey); // key
sbCookies.append("\t");
sbCookies.append(strValue); // value
sbCookies.append('\n');
}
return sbCookies.toString();
}
private static String str2Seconds(String strTime) {
Calendar c = Calendar.getInstance();
long millisSecond = 0;
try {
if(strTime.contains("-0000"))
c.setTime(new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss zzzzz", Locale.US).parse(strTime));
else
c.setTime(new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm:ss zzz", Locale.US).parse(strTime));
millisSecond = c.getTimeInMillis() / 1000;
} catch (ParseException e) {
millisSecond = -1;
Log.e("QuickHTTPInterface", e.toString());
e.printStackTrace();
}
if (-1 == millisSecond) {
return strTime;
}
return Long.toString(millisSecond);
}
}