8

I am trying to make SOAP Call via OkHttp library so I can upgrade to Retrofit Library use . I have gone through said post .

OkHttp . Link

Library Used in Gradle

compile 'com.squareup.okhttp3:okhttp:3.4.1'

I have successfully made the call , but the Response Code comes as 415 . I have changed the content type as

Have tested with

.addHeader("Content-Type", "text/xml; charset=utf-8")

and

.addHeader("Content-Type", "application/json"; charset=utf-8")

and

.addHeader("Content-Type", "html/text"; charset=utf-8")

also

What could be the problem

My Request Format

final Request request = new Request.Builder()
                .url(URL)
                .addHeader("Content-Type", "text/xml; charset=utf-8")
                .addHeader("soapaction", "http://tempuri.org/Login")
                .post(body)
                .build();

Other Code :

final OkHttpClient client = new OkHttpClient();
        soapRequest = Login(userName.getText().toString(),password.getText().toString());
        RequestBody body = RequestBody.create(SOAP_MEDIA_TYPE, soapRequest);

        final Request request = new Request.Builder()
                .url(URL)
                .addHeader("Content-Type", "text/xml; charset=utf-8")
                .addHeader("soapaction", "http://tempuri.org/Login")
                .post(body)
                .build();


        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String mMessage = e.getMessage().toString();
                Log.w("failure Response", mMessage);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String mMessage = response.body().string();

                Log.i("",mMessage);

             //   Toast.makeText(MainActivity.this,"Result :"+mMessage,Toast.LENGTH_LONG).show();

                //code = response.code();
              //  getResponse(mMessage, response);

            }
        });

Function Login

 public  String Login(String name,String password) {

        String body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "  <soap:Body>\n" +
                "    <Login xmlns=\"http://tempuri.org/\">\n" +
                "      <Name>"+userName+"</Name>\n" +
                "      <Password>"+password+"</Password>\n" +
                "    </Login>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";

        return body;
    }
5
  • I think problem with soapRequest = Login(userName.getText().toString(),password.getText().toString()); please try to fix it stackoverflow.com/a/34902666/2298357 Commented Jan 21, 2018 at 13:23
  • Please check its my soap request Commented Jan 21, 2018 at 13:26
  • Have you tried "Content-Type: application/soap+xml; charset=utf-8"? Commented Jan 21, 2018 at 13:39
  • Still gave the same error : 415 Unsupported Media Type Commented Jan 21, 2018 at 17:46
  • If I try it with HTTP request it works fine ... but not with OkHttp library Commented Jan 21, 2018 at 17:46

1 Answer 1

9

Try the below code

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("text/xml");

String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
            " <soap:Body>\n" + " <Login xmlns=\"http://tempuri.org/\">\n" +
            " <Name>"+userName+"</Name>\n" + " <Password>"+password+"</Password>\n" +
            " </Login>\n" + " </soap:Body>\n" + "</soap:Envelope>";

RequestBody body = RequestBody.create(mediaType, soap);
Request request = new Request.Builder()
            .url(YOUR_LINK)
            .post(body)
            .addHeader("content-type", "text/xml")
            .build();

Response response = client.newCall(request).execute();
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to create a request with attachment in soup?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.