我終於把我的應用程序「看似」發佈到https。但是,每次我發佈並得到結果,結果都是-200。這個結果是服務器告訴我需要進行身份驗證的響應。如果我正確認證,結果將是正面的。而且我正在發帖進行身份驗證......所以回覆只是告訴我它失敗了。我曾與服務器管理員談過,他說我可能在某個地方有實際的空間。https發帖,未收到期望值
我的問題是,我如何檢查正在發佈的完整消息?基本上,我如何檢查以確保我在網絡瀏覽器中輸入的URL與應用程序中發佈的內容相同?現在,我已經打印了一些內容,但是我怎麼知道所有這些信息都是正確的。從「https」開始直到消息的最後。
在此先感謝!任何幫助表示讚賞,如果您發現我目前有任何問題,請告訴我!謝謝!
的URL看起來應該是這樣發帖時:我從來沒有聽說過
https://ipaddress/health_monitoring/admin.php?action=authentication&username=uName&password=pWord
//my database helper class
public class SmartDBHelper {
private static SmartDBHelper sDBHObject;
private SmartDBHelper() {
}
public static synchronized SmartDBHelper getSDBHObject() {
if(sDBHObject == null) {
sDBHObject = new SmartDBHelper();
}
return sDBHObject;
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
/* this function is to authenticate with the database
* it returns the id_subject, if it is greater than 0
* authentication was successful.
*/
public static synchronized int authenticate(String uName, String pWord) {
Map<String, String> tempMap = new LinkedHashMap<String, String>();
tempMap.put("action", "authentication");
tempMap.put("username", "uName");
tempMap.put("password", "pWord");
try {
String tempUrl = "https://ipaddress/health_monitoring/admin.php";
String result = post(tempUrl, tempMap);
Log.v("smartdbhelper post result", result);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
// always verify the host - dont check for certificate
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
/**
* Trust every server - dont check for any certificate
*/
private static void trustAllHosts() {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
private static String post(String urlString, Map formParameters)
throws MalformedURLException, ProtocolException, IOException {
DataOutputStream ostream = null;
trustAllHosts();
URL tempUrl;
tempUrl = new URL(urlString);
HttpsURLConnection https = (HttpsURLConnection) tempUrl.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
https.setRequestMethod("POST");
https.setDoInput(true);
https.setDoOutput(true);
ostream = new DataOutputStream(https.getOutputStream());
if(formParameters != null) {
Set parameters = formParameters.keySet();
Iterator it = parameters.iterator();
StringBuffer buf = new StringBuffer();
for(int i = 0, paramCount = 0; it.hasNext(); i++) {
String parameterName = (String) it.next();
String parameterValue = (String) formParameters.get(parameterName);
if(parameterValue != null) {
parameterValue = URLEncoder.encode(parameterValue);
if(paramCount > 0) {
buf.append("&");
}
buf.append(parameterName);
buf.append("=");
buf.append(parameterValue);
++paramCount;
}
}
Log.v("smartdbhelper adding post parameters", buf.toString());
Log.v("smartdbhelper adding post parameters", https.toString());
ostream.writeBytes(buf.toString());
}
if(ostream != null) {
ostream.flush();
ostream.close();
}
Object contents = https.getContent();
InputStream is = (InputStream) contents;
StringBuffer buf = new StringBuffer();
int c;
while((c = is.read()) != -1) {
buf.append((char)c);
Log.v("smartdbhelper bugger", buf.toString());
}
https.disconnect();
return buf.toString();
}
}
剝離掉所有避免SSL名稱驗證的代碼將有助於提高您的問題的可讀性。 – 2011-04-06 21:09:33
我在那裏留下了那些東西,以便人們可以檢查可能影響問題的代碼的完整部分,因爲我不確定它是否可以正常工作。在發生此問題之前,我無法發佈到https。 – prolink007 2011-04-06 21:52:10