有以下代码,用于从Android应用程序(用于测试服务)调用 1C Web服务 . 但是,当您调用Web服务时,代码会抛出 exceptionHttpProtocolException . 可能是什么问题呢?

这可能是由于授权 (Base64.DEFAULT instead of Base64.NO_WRAP) 的数据输入不正确,但修复没有帮助 .

package pro.oksi.android.myapplication;

import android.app.Activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Text;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;

interface callback {
    void callback(String response);
}

public class MainActivity extends AppCompatActivity implements callback {
    private Activity act;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        act = this;

        Button ok = (Button) findViewById(R.id.button);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText urlView = (EditText) findViewById(R.id.url);
                EditText nameView = (EditText) findViewById(R.id.name);
                EditText namespaceView = (EditText) findViewById(R.id.namespace);
                EditText usernameView = (EditText) findViewById(R.id.username);
                EditText passwordView = (EditText) findViewById(R.id.password);

                if(urlView.getText().toString() == null) {
                    Log.i("Null", "null");
                }

                SOAP task = new SOAP(act, namespaceView.getText().toString(), urlView.getText().toString(), nameView.getText().toString(), usernameView.getText().toString(), passwordView.getText().toString());
                task.execute();
            }
        });
    }

    @Override
    public void callback(String arg) {
        TextView response = (TextView) findViewById(R.id.textView);
        response.setText(arg);
    }
}

class SOAP extends AsyncTask<Void, Void, Void> {

    private String username;
    private String password;
    private String namespace;
    private String url;
    private String action;
    private String webservice;
    private String method;
    private Activity context;
    private callback inter;
    private String response = "";

    public SOAP(Activity context, String namespace, String url, String webservice, String username, String password) {

        this.context = context;
        this.inter = (callback) context;
        this.username = username;
        this.password = password;
        this.url = url;
        this.namespace = namespace;
        this.webservice = webservice;
        this.method = "ServiceInfo";
        this.action = this.namespace + "#" + this.webservice + ":" + this.method;

    }

    protected void onPreExecute() {
        super.onPreExecute();
    }

    protected Void doInBackground(Void... args) {
        try {
            String request = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:utr=\"http://www.oksi.pro/?utr_siteonline\">" +
                "   <soapenv:Header/>" +
                "   <soapenv:Body>" +
                "   <utr:ServiceInfo/>" +
                "   </soapenv:Body>" +
                "</soapenv:Envelope>";
        StringEntity se = new StringEntity(request, HTTP.UTF_8);
        se.setContentType("text/xml");
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(new URI(this.url));
        httpPost.addHeader("Content-type", "text/xml;charset=UTF-8");
        httpPost.addHeader("Accept-Encoding", "gzip,deflate");
        httpPost.addHeader("SOAPAction", this.action);
        httpPost.addHeader("Host", "84.42.19.27:60606");
        httpPost.addHeader("Content-Length", request.length() + "");
        httpPost.addHeader("Connection", "Keep-Alive");
        httpPost.addHeader("User-Agent", "Apache-HttpClient/4.1.1 (java 1.5)");

        String auth = this.username + ":" + this.password;
        byte[] authRaw = auth.getBytes();
        String authEncoded = Base64.encodeToString(authRaw, 0, authRaw.length, Base64.NO_WRAP);

        httpPost.addHeader("Authorization", "Basic " + authEncoded);
        httpPost.setEntity(se);
        HttpResponse response = client.execute(httpPost);
        int respCode = response.getStatusLine().getStatusCode();
        if(respCode >= 200 && respCode < 300) {
            this.response = EntityUtils.toString(response.getEntity());
            Log.i("OK", this.response);
        } else {
            this.response = "Code" + respCode;
        }
        } catch (UnsupportedEncodingException Ex) {
            this.response = "Unsupported encoding exception";
        } catch (URISyntaxException Ex) {
            this.response = "URI syntax exception";
        } catch (ClientProtocolException Ex) {
            this.response = "Client protocol exception";
        } catch (IOException Ex) {
            this.response = "IO exception";
        }
        return null;
    }

protected void onPostExecute(Void arg) {
    inter.callback(this.response);
}

}