Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Settings http headers in Java 6 SE httpserver

I try to publish Atom feed (generated with Rome) using Java 6 SE httpserver. For correct feed discovery in FireFox I need custom headers.

This is my code:

 Headers headers=e.getRequestHeaders();
 ArrayList<String>list=new ArrayList<String>();
 list.add("application/atom+xml");
 headers.put("content-type", list);
 e.sendResponseHeaders(200, 0);

Unfortunately feed is displaying like xml (browser doesn't, ask me what to do with feed) and sniffing with livehttpheaders shows that there isn't content-type attribute.

like image 751
Maciek Sawicki Avatar asked Dec 01 '09 19:12

Maciek Sawicki


People also ask

How do I set HTTP headers?

In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name. In the Value box, type the custom HTTP header value.

How do I add HTTP header to request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers. For example: oRequest = RequestBuilder:Build('GET', oURI) :AddHeader('MyCustomHeaderName','MyCustomHeaderValue') :AddHeader('MySecondHeader','MySecondHeaderValue') :Request.

How do I use custom HTTP headers?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

What is HTTP header in Java?

The HTTP headers are used to pass additional information between the clients and the server through the request and response header. All the headers are case-insensitive, headers fields are separated by colon, key-value pairs in clear-text string format.


1 Answers

You can set the response headers like this:

Headers headers = exchange.getResponseHeaders();
headers.add("Content-Type", "application/atom+xml");
exchange.sendResponseHeaders(200, 0);
like image 79
Tony Edgecombe Avatar answered Sep 28 '22 03:09

Tony Edgecombe