【发布时间】:2011-07-31 04:13:21
【问题描述】:
我终于让我的申请“看似”发布到 https。但是,每次我发布并获得结果时,结果都是-200。这个结果是来自服务器的响应,告诉我我需要进行身份验证。如果我正确验证,结果将是肯定的。我正在发布以进行身份验证......所以响应只是告诉我它失败了。我已经和服务器管理员谈过了,他说我可能在实际帖子的某个地方有一个空间。
我的问题是,我如何查看发布的完整消息?基本上,我如何检查以确保我在网络浏览器中的 URL 中输入的内容与应用程序中发布的内容相同?现在,我让它打印了一些,但我怎么知道所有这些信息都是正确的。从“https”开始一直到消息的最后。
提前致谢!!任何帮助表示赞赏,如果您发现我目前所拥有的有任何问题,请告诉我!谢谢!
发帖时网址应如下所示:
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 名称验证的代码将有助于使您的问题更具可读性。
-
我把这些东西留在了那里,这样人们就可以检查我的代码中可能影响问题的全部部分,因为我不确定它是否能正常工作。在此问题开始发生之前,我在发布到 https 时遇到了问题。
标签: java android http https http-post