Setting custom header using HttpURLConnection

别等时光非礼了梦想. 提交于 2019-12-03 07:04:59

The conn.getHeaderField("CustomHeader") returns the response header not the request one.

To return the request header use: conn.getRequestProperty("CustomHeader")

It is a good idea to send

conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("CustomHeader", token);

instead of

// Set Headers
conn.setRequestProperty("CustomHeader", "someValue");
conn.setRequestProperty("accept", "application/json");

Both the type value and header should be changed. it works in my case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!