我终于让我的应用程序“看似”发布到https。但是,每次发布并获得结果时,结果为-200。这个结果是来自服务器的响应,告诉我需要进行身份验证。如果我正确验证,结果将是积极的。我发帖进行身份验证......所以回复只是告诉我它失败了。我已经和服务器管理员谈过,他说我可能在实际的帖子里有一个空间。
我的问题是,如何查看正在发布的完整邮件?基本上,我如何检查以确保我在Web浏览器中放入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();
}
}
答案 0 :(得分:0)
我从未听说过-200表示需要进行身份验证。这是您的服务器发送的自定义响应代码吗?通常,当您需要进行身份验证时,您将获得401等。
此外,当您发布参数时,它们不会添加到URL的末尾,因此您的URL看起来与您在问题顶部的内容不同。
它只是https://ipaddress/health_monitoring/admin.php
答案 1 :(得分:0)
我刚才注意到的一件事是:
tempMap.put("username", "uName");
tempMap.put("password", "pWord");
您将常量值"uName"
和"pWord"
放入地图而不是变量。它应该是:
tempMap.put("username", uName);
tempMap.put("password", pWord);
您可能还想首先在params上调用.trim()
以确保没有额外的空格。
答案 2 :(得分:0)
我弄清楚我的问题是什么!在我将帖子附加到网址之前,我实际上是发布到http ... oops。
下面列出了需要示例的帖子的固定代码。
private static String post(String urlString, Map formParameters)
throws MalformedURLException, ProtocolException, IOException {
DataOutputStream ostream = null;
trustAllHosts();
URL tempUrl;
StringBuffer buf = new StringBuffer();
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());
}
urlString = urlString + "?" + buf;
Log.v("smartdbhelper url string", urlString);
tempUrl = new URL(urlString);
HttpsURLConnection https = (HttpsURLConnection) tempUrl.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
Log.v("smartdbhelper adding post parameters", https.toString());
https.setRequestMethod("POST");
https.setDoInput(true);
https.setDoOutput(true);
ostream = new DataOutputStream(https.getOutputStream());
ostream.writeBytes(buf.toString());
if( ostream != null ) {
ostream.flush();
ostream.close();
}
Object contents = https.getContent();
InputStream is = (InputStream) contents;
StringBuffer buf2 = new StringBuffer();
int c;
while((c = is.read()) != -1) {
buf2.append((char)c);
Log.v("smartdbhelper bugger", buf2.toString());
}
https.disconnect();
return buf2.toString();
}