1

After some hours research, I haven't found any solution that contains my problem.

I have a String value that I want to send it via POST.

I've build something. I just don't know how to set that it will be send as binary/octet-stream.

String data = someData();
String sUrl = "http://some.example.website.com";
URL url = new URL(sUrl);
HttpURLConnection connection = (HttpURLConnection) (new URL(sUrl)).openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();

I'm using HttpUrlConnection because I used DefaultHttpClient() that is deprecated.

I hope someone can help me!

Kind Regards!

2
  • what is the error that you are getting?. Also you can use the DefaultHttpClient(), eve though it is deprecated. If you want I can share the DefaultHttpClient() code that I use in my app. Commented Mar 3, 2016 at 11:08
  • Yes share it please. I thought i replace DefaultHttpClient() with HttpUrlConnection. As i see I cannot do, what I want. Please share your code Commented Mar 3, 2016 at 11:09

1 Answer 1

1

You can use code like the below snippet:

HttpClient httpclient = new DefaultHttpClient();

String responseXml = null;
StringEntity se = "test string to be sent to the server";


HttpPost httppost = new HttpPost(temp);
try {
    se.setContentType("text/soap+xml");
    httppost.setEntity(se);
    HttpResponse httpresponse = httpclient.execute(httppost);
    HttpEntity resEntity = httpresponse.getEntity();
    responseXml = EntityUtils.toString(resEntity);
    Log.d("Response XML", responseXml);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    Log.e(TAG,e.getMessage());
} catch (ClientProtocolException e) {
    Log.e(TAG,e.getMessage());
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
    Log.e(TAG, e.getMessage());
} catch (Exception e){
    e.printStackTrace();
    Log.e(TAG,e.getMessage());
}

I used this code to send a xml to the server. You can replace what you want to send in the se variable.

Sign up to request clarification or add additional context in comments.

Comments

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.