Sending HTTP Header Information Using Java UrlConnection

Ok, so I have this bit of code, and when I make a request, I would like to add some HTTP header information. How can I do it?

public boolean call(String apiCall) { if (this.apiCalls.containsKey(apiCall)) { try{ URL url = this.apiCalls.get(apiCall); url = new URL(url.toString() + "?memberid=76710"); URLConnection urlConn = url.openConnection(); InputStream is = urlConn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while((current = bis.read()) != -1){ baf.append((byte)current); } this.responseResultText = new String(baf.toByteArray()); return true; } catch(Exception e){ this.responseResultText = e.getMessage(); return false; } } this.responseResultText = "API call " + apiCall + " doesn't exist."; return false; } 

Thanks!

+4
source share
1 answer

Use URLConnection#setRequestProperty() .

 connection.setRequestProperty("Content-Type", "text/plain"); 

See also:

+10
source

Source: https://habr.com/ru/post/1315012/


All Articles