简体   繁体   English

使用Java UrlConnection发送HTTP头信息

[英]Sending HTTP Header Info with Java UrlConnection

Alright, so I have this bit of code, and when I make the request, I would like to include some HTTP Header info. 好吧,所以我有这些代码,当我提出请求时,我想包含一些HTTP标头信息。 How would I go about doing that? 我该怎么做呢?

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! 谢谢!

Use URLConnection#setRequestProperty() . 使用URLConnection#setRequestProperty()

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

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM