Java https过帐,因此未收到预期值
我终于得到了我的应用程序“似乎”张贴到https。然而,每次我发布并得到结果时,结果是-200。这个结果是服务器告诉我需要进行身份验证的响应。如果我的身份验证正确,结果将是肯定的。我正在发帖验证。。。因此,答案只是告诉我它失败了。我已经和服务器管理员谈过了,他说我可能在实际帖子的某个地方有一个空间 我的问题是,如何检查正在发布的完整消息?基本上,我如何检查以确保我在web浏览器的URL中输入的内容与应用程序中发布的内容相同?现在,我让它打印了一些,但是我怎么知道所有的信息都是正确的呢。从“https”开始,一直到消息的最后 提前谢谢!!非常感谢您的帮助,如果您发现我目前所拥有的有任何问题,请告诉我!谢谢大家! 发布时,URL应如下所示:Java https过帐,因此未收到预期值,java,android,http,https,http-post,Java,Android,Http,Https,Http Post,我终于得到了我的应用程序“似乎”张贴到https。然而,每次我发布并得到结果时,结果是-200。这个结果是服务器告诉我需要进行身份验证的响应。如果我的身份验证正确,结果将是肯定的。我正在发帖验证。。。因此,答案只是告诉我它失败了。我已经和服务器管理员谈过了,他说我可能在实际帖子的某个地方有一个空间 我的问题是,如何检查正在发布的完整消息?基本上,我如何检查以确保我在web浏览器的URL中输入的内容与应用程序中发布的内容相同?现在,我让它打印了一些,但是我怎么知道所有的信息都是正确的呢。从“htt
//我的数据库助手类
公共类SmartDBHelper{
私有静态SmartDBHelper sDBHObject;
私有SmartDBHelper(){
}
公共静态同步SmartDBHelper getSDBHObject(){
如果(sDBHObject==null){
sDBHObject=新的SmartDBHelper();
}
返回sDBHObject;
}
公共对象克隆()引发CloneNotSupportedException{
抛出新的CloneNotSupportedException();
}
/*此函数用于对数据库进行身份验证
*如果id_subject大于0,则返回该id_subject
*身份验证成功。
*/
公共静态同步int身份验证(字符串uName、字符串pWord){
Map tempMap=newlinkedhashmap();
tempMap.put(“操作”、“身份验证”);
tempMap.put(“用户名”、“uName”);
tempMap.put(“密码”、“密码”);
试一试{
字符串tempUrl=”https://ipaddress/health_monitoring/admin.php";
字符串结果=post(tempUrl、tempMap);
Log.v(“smartdbhelper post结果”,结果);
}捕获(例外e){
e、 printStackTrace();
}
返回0;
}
//始终验证主机-不检查证书
最终静态HostnameVerifier DO\u NOT\u VERIFY=新HostnameVerifier(){
公共布尔验证(字符串主机名、SSLSession会话){
返回true;
}
};
/**
*信任每台服务器-不检查任何证书
*/
私有静态void trustAllHosts(){
//创建不验证证书链的信任管理器
TrustManager[]trustAllCerts=new TrustManager[]{new X509TrustManager(){
public java.security.cert.X509Certificate[]getAcceptedIssuers(){
返回新的java.security.cert.X509Certificate[]{};
}
public void checkClientTrusted(X509Certificate[]链,
字符串authType)引发CertificateException{
}
公共无效checkServerTrusted(X509Certificate[]链,
字符串authType)引发CertificateException{
}
} };
//安装所有信任管理器
试一试{
SSLContext sc=SSLContext.getInstance(“TLS”);
sc.init(null,trustAllCerts,new java.security.SecureRandom());
httpsurl连接
.setDefaultSSLSocketFactory(sc.getSocketFactory());
}捕获(例外e){
e、 printStackTrace();
}
}
私有静态字符串post(字符串urlString,映射formParameters)
抛出畸形的DurException、ProtocolException、IOException{
DataOutputStream ostream=null;
trustAllHosts();
URL-tempUrl;
tempUrl=新URL(urlString);
HttpsURLConnection https=(HttpsURLConnection)tempUrl.openConnection();
https.setHostnameVerifier(不验证);
https.setRequestMethod(“POST”);
https.setDoInput(true);
https.setDoOutput(true);
ostream=newdataoutputstream(https.getOutputStream());
if(formParameters!=null){
Set parameters=formParameters.keySet();
迭代器it=参数。迭代器();
StringBuffer buf=新的StringBuffer();
for(int i=0,paramCount=0;it.hasNext();i++){
字符串参数name=(String)it.next();
String parameterValue=(String)formParameters.get(parameterName);
if(参数值!=null){
parameterValue=URLEncoder.encode(parameterValue);
如果(参数计数>0){
buf.追加(“&”);
}
buf.append(参数名称);
buf.追加(“=”);
buf.append(参数值);
++参数计数;
}
}
v(“smartdbhelper添加post参数”,buf.toString());
v(“smartdbhelper添加post参数”,https.toString());
ostream.writeBytes(buf.toString());
}
if(ostream!=null){
ostream.flush();
ostream.close();
}
对象内容=https.getContent();
InputStream为=(InputStream)内容;
StringBuffer buf=新的StringBuffer();
INTC;
而((c=is.read())!=-1){
追加((char)c);
Log.v(“smartdbhelper bugger”,buf.toString());
}
https.disconnect();
返回buf.toString();
}
}
我从未听说过-200表示需要进行身份验证。这是发送的自定义响应代码吗
//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();
}
}
tempMap.put("username", "uName");
tempMap.put("password", "pWord");
tempMap.put("username", uName);
tempMap.put("password", pWord);
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();
}