0

HTTPS 接続を試みていますが、問題があります。

サーバーに接続しようとするとタイムアウトが発生しますが、一部のネットワークでのみ発生します。私のアプリも iOS 用に実装していますが、ここではサーバーの応答は OK です。

これが私のコードです:

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