Web Science/Part1: Foundations of the web/Web Architecture/Hypertext Transfer Protocol/Content negotiation

From Wikiversity
Jump to: navigation, search

New-here.png NewDiscussionButton.png Feed-icon.svg



Quick Navigation: Video - Script - further Reading - Quiz - Discussion


Video of the lesson: Content negotiation

Add-to-watchlist-HOCH.png

Quick Navigation: Video - Script - further Reading - Quiz - Discussion

Script for: Content negotiation

Mainly the following function was added to SimpleWebServer.java. There are some slighte adoptions to the Request class but this is mainly code completion displayed inside the video.

	private static String fetchAsciiFile(String path, String accept) {
		try {
			StringBuilder body = new StringBuilder(100000);
			BufferedReader br = new BufferedReader(new FileReader(new File(System.getProperty("user.dir") + "/webdirectory" + path)));
			String tmp = null; 
 
			if (accept != null && accept.equals("text/xml")){
				body.append("<?xml version=\"1.0\" encoding=\"ASCII\" standalone=\"yes\"?>\n<playlist>\n");
			}
 
			while ((tmp = br.readLine())!= null){
				if (accept == null)
					body.append(tmp + "\n");
				else if (accept.equals("text/plain")){
					body.append(tmp + "\n");
				} else if (accept.equals("text/xml")){
					String []values = tmp.split(" - ");
					body.append("<track id=\""+values[1]+"\">\n\t<time>"+values[0]+"</time>\n\t<artist>"+values[2]+"</artist>\n\t<title>"+values[3]+"</title>\n</track>\n");
				}
			}
			if (accept != null && accept.equals("text/xml")){
				body.append("</playlist>\n");
			}
 
			br.close();
			return body.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

Quick Navigation: Video - Script - further Reading - Quiz - Discussion

Further information, readings and exercises about Content negotiation


Quick Navigation: Video - Script - further Reading - Quiz - Discussion

Check your understanding of: Content negotiation

1. Which of the following HTTP requests should be used to request an XML file from a server

GET /file HTTP/1.0\r\nAccept: text/xml\r\n\r\n
GET /file?type=xml HTTP/1.0\r\n\r\n
GET /file.xml HTTP/1.0\r\n\r\n
GET /file.xml HTTP/1.0\r\nAccept: text/xml\r\n\r\n

2. Which of the following HTTP requests would technically work (if implemented) to request an XML file from a server

GET /file HTTP/1.0\r\nAccept: text/xml\r\n\r\n
GET /file?type=xml HTTP/1.0\r\n\r\n
GET /file.xml HTTP/1.0\r\n\r\n
GET /file.xml HTTP/1.0\r\nAccept: text/xml\r\n\r\n

3. What is the main aim of content negotiation?

human to machine communication over http
human to human communication over http
machine to machine communication over http
All of the above

Your score is 0 / 0


Quick Navigation: Video - Script - further Reading - Quiz - Discussion

Discuss Content negotiation with others

QuestionButton.png DiscussionButton.png

Join the discussion: Ask and questions and provide answers

  • If you click the ask a question button you're question will appear at the bottom of this list.
  • If you click the give an answer you will be able to provide an answer to the questions that have been asked so far.

QuestionButton.png DiscussionButton.png


Previous-mooc-lesson.png Parent-mooc-lesson.png Next-mooc-lesson.png

navigational context


Video
and script
Associated Lesson
Making HTTP requests
A Simple Web Client
A Simple Web Server
The HTTP Header
Content negotiation
Summary, Further readings, Homework

The following video of the flipped classroom associated with this topic are available:

You can find more information on wiki commons and also directly download this file