Sockets POST over SSL/HTTPS REST Api Android正在响应400个错误请求

Sockets POST over SSL/HTTPS REST Api Android正在响应400个错误请求,sockets,post,ssl,restful-authentication,Sockets,Post,Ssl,Restful Authentication,在我的应用程序中,我想从使用REST服务的远程服务器中发布android应用程序XML数据。我的代码如下: String url = "api.example.com"; int port = 443; String query = "<?xml version='1.0' encoding='UTF-8'?><request><client><name>APIappDevAccount</name><passw

在我的应用程序中,我想从使用REST服务的远程服务器中发布android应用程序XML数据。我的代码如下:

 String url = "api.example.com";
    int port = 443;
    String query = "<?xml version='1.0' encoding='UTF-8'?><request><client><name>APIappDevAccount</name><password>123456</password></client><user><name>foyzulkarim</name><password>123456</password><groupId>12345</groupId></user></request>";
    Socket socket = null;
    try {
    socket =  new Socket(url,port);
    } catch (UnknownHostException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            } catch (IOException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
    BufferedReader br = null;
            try {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));                        
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            PrintStream pw = null;
            try {
                pw = new PrintStream(socket.getOutputStream());

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            pw.print("POST api.example.com/rest/rest/user");
    pw.print("Content-Type: application/xml");
            pw.print("Content-Length:" + query.length());
            pw.print(query);
            System.out.println("hello foysal.");
            //get result
            String l = null;
            String text="";

            try {
                while ((l=br.readLine())!=null) {
                    System.out.println(l);
            text+=l;        
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            pw.close();
            try {
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
我有个例外

javax.net.ssl.SSLException: Not trusted server certificate
java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
排队

br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
我的问题是,在上述场景中,如何通过SSL从android应用程序发布数据? 我想我们中的许多人都面临着这个问题,所以我请求你给我/我们一些详细的答案。干杯。

太多未知:-)

对您控制的测试服务器尝试普通HTTP。 我的直觉是,它也会出现同样的错误。 例如,您似乎没有在HTTP头和正文之间放置空行

你为什么要实现HTTP呢?不要告诉我在任何平台上都没有可以使用的API或库?通常有java.net.HttpUrlConnection或ApacheHttpClient

编辑:


嘿,看看谷歌带来了什么:

看来我需要添加信任锚证书才能验证整个密钥链。 我已经向我的API提供商请求了有关此证书的信息,他们给了我一个可以从中获取证书的web链接。[为了保密,我更改了web链接]

他们还告诉我通过(我想应该从命令提示符下执行)获取并尝试连接

然后,我必须将证书集成到我的应用程序中


我现在将尝试了解如何集成该证书,但讨论应该在另一个问题中进行

我已经做到了。代码如下所示

private String executeRequest(String targetURL, final String requestMethod,
            String soap_request_message_header, String soap_request_message_body) {
        URL url;
        HttpURLConnection connection = null;
        try {
            url = new URL(targetURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(requestMethod);

            connection.setRequestProperty("SOAPAction",
                    soap_request_message_header);

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            // Send request
            DataOutputStream wr = new DataOutputStream(connection
                    .getOutputStream());
            wr.writeBytes(soap_request_message_body);
            wr.flush();
            wr.close();

            // Get Response
            InputStream is;
            final int responseCode = connection.getResponseCode();
            Log.i("response", "code=" + responseCode);
            if (responseCode <= 400) {
                is = connection.getInputStream();
            } else {
                /* error from server */
                is = connection.getErrorStream();
            }
            // is= connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            Log.i("response", "" + response.toString());
            return response.toString();

        } catch (Exception e) {

            Log.e("error https", "", e);
            return e.getMessage();

        } finally {

            if (connection != null) {
                connection.disconnect();
            }
        }
    }
private String executeRequest(字符串targetURL,最终字符串请求方法,
字符串soap请求消息头、字符串soap请求消息体){
网址;
HttpURLConnection=null;
试一试{
url=新url(targetURL);
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod(requestMethod);
connection.setRequestProperty(“SOAPAction”,
soap_请求_消息_头);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(真);
//发送请求
DataOutputStream wr=新的DataOutputStream(连接
.getOutputStream());
writeBytes(soap请求消息体);
wr.flush();
wr.close();
//得到回应
输入流为;
final int responseCode=connection.getResponseCode();
Log.i(“响应”、“代码=“+responseCode”);

如果(responseCode如果我使用HttpClient,我找不到任何关于Delete或Update命令的示例。更重要的是,发布xml是api的主要要求。该服务是否通过普通HTTP接受您的请求?400错误请求表明SSL工作正常,但您手工制作的HTTP格式不正确。PrintStream.print是否甚至使用ne终止每个标头wline?我仍然建议使用Apache HttpClient,因为它似乎已经在您的平台上可用,而不会增加应用程序的占用空间。它支持比我所知道的更多的方法:正如您所看到的,我想要的是在线程中详细阐述的,我想发布XML。这里提到的问题已经解决,但我现在想要与SSL相关的解决方案。这就是我在这里问的原因。谢谢你的帮助。
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
openssl s_client -connect api.example.com:443 -CAfile /root/SSL_bundle.pem
private String executeRequest(String targetURL, final String requestMethod,
            String soap_request_message_header, String soap_request_message_body) {
        URL url;
        HttpURLConnection connection = null;
        try {
            url = new URL(targetURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(requestMethod);

            connection.setRequestProperty("SOAPAction",
                    soap_request_message_header);

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            // Send request
            DataOutputStream wr = new DataOutputStream(connection
                    .getOutputStream());
            wr.writeBytes(soap_request_message_body);
            wr.flush();
            wr.close();

            // Get Response
            InputStream is;
            final int responseCode = connection.getResponseCode();
            Log.i("response", "code=" + responseCode);
            if (responseCode <= 400) {
                is = connection.getInputStream();
            } else {
                /* error from server */
                is = connection.getErrorStream();
            }
            // is= connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            Log.i("response", "" + response.toString());
            return response.toString();

        } catch (Exception e) {

            Log.e("error https", "", e);
            return e.getMessage();

        } finally {

            if (connection != null) {
                connection.disconnect();
            }
        }
    }