0

我正在尝试进行 HTTPS 连接,但我遇到了问题。

当我尝试与服务器连接时,我会超时,但仅限于某些网络。我的应用程序也在为 iOS 实现,这里服务器的响应是好的。

这是我的代码:

public static String connectToServer(String xmlBody) {

        InputStream is = null;
        String responseString = "";
        try {
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 40000);
            HttpConnectionParams.setSoTimeout(httpParameters, 40000);

            DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);

            HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false); // I tried with this

            CredentialsProvider credProvider = new BasicCredentialsProvider();
            credProvider.setCredentials(new AuthScope(HOST, PORT), new UsernamePasswordCredentials(USERNAME, PASSWORD));
            httpclient.setCredentialsProvider(credProvider);
            HttpPost httpPost = new HttpPost(URL);

            httpPost.setHeader("Content-type", "application/xml; charset=utf-8");

            StringEntity se = new StringEntity(xmlBody, HTTP.UTF_8);
            se.setContentType("application/xml");
            httpPost.setEntity(se);

            HttpResponse response = httpclient.execute(httpPost);


            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            responseString = IOUtils.toString(is, "UTF-8");
            Log.i("RESPONSE", responseString);

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
            // ERROR;
            return null;
        }
        // SUCCESS;
        return responseString;
    }

我也试过这个:

public String connectToServer(String xmlBody) {

            String responseString = "";

            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(openURLForInput(new URL(URL), USERNAME, PASSWORD, xmlBody)));
                String line;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                    responseString += line;
                }

            } catch (IOException e) {
                e.printStackTrace();
                responseString = e.toString();
            }

            return responseString;
        }


public InputStream openURLForInput(URL url, String uname, String pword, String xmlBody) throws IOException {

            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Authorization", userNamePasswordBase64(uname, pword)); //this is OK

            conn.addRequestProperty("Content-type", "application/xml; charset=utf-8");



            conn.setRequestMethod("POST");
            conn.connect();
            OutputStream output = conn.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(output, "UTF-8");
            BufferedWriter writer = new BufferedWriter(osw);
            writer.write(xmlBody);

            writer.close();
            output.close();


            int status = conn.getResponseCode();
            Log.i("STATUS", status + "");
            Log.i("STATUS_ERROR", conn.getResponseMessage());

            return conn.getInputStream();
        }
4

0 回答 0