Android set content type HttpPost

前端 未结 2 440
花落未央
花落未央 2021-02-07 01:42

How can you change the content-type of a HttpPost in android?

For a request I need to set the content type to application/x-www-form-urlencoded

So i got this bit

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 02:40

                HttpPost httppost = new HttpPost(builder.getUrl());
                httppost.setHeader(HTTP.CONTENT_TYPE,
                        "application/x-www-form-urlencoded;charset=UTF-8");
                // Add your data
                httppost.setEntity(new UrlEncodedFormEntity(builder
                        .getNameValuePairs(), "UTF-8"));
    
                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
    

    Note: the builder just contains url and namevalue pairs.

提交回复
热议问题