parse Request Header : Http Connection « Network « Android






parse Request Header

     

//package jp.tf_web.httpserversample.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.RequestLine;
import org.apache.http.StatusLine;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicLineParser;
import org.apache.http.message.BufferedHeader;
import org.apache.http.util.CharArrayBuffer;

import android.util.Log;
import android.webkit.MimeTypeMap;

class HttpUtil {
  public final static String HTTP_PROTOCOL = "HTTP/1.1";
  public final static String HEADER_NAME_REQUESTLINE = "RequestLine";
  public final static String HEADER_NAME_CONTENT_LENGTH = "Content-Length";
  
  private HttpUtil(){}
  
  //??????????????
  public static HashMap<String,Object> parseRequestHeader(String headers_str) throws IOException{
    //????????
    StringReader sr = new StringReader(headers_str);
    BufferedReader bur = new BufferedReader(sr);

    HashMap<String,Object> headerList = new HashMap<String,Object>();
    String line;
    int line_index = 0;
    while((line = bur.readLine()) != null){
      if(line.length() == 0) break;
      
      //????????
      if(line_index == 0){
        //????????
        BasicLineParser parser = new BasicLineParser();
        RequestLine request_line = BasicLineParser.parseRequestLine(line, parser);
        headerList.put(HttpUtil.HEADER_NAME_REQUESTLINE,request_line);
      }
      else{
        //????
        CharArrayBuffer cab = new CharArrayBuffer(line.length());
        cab.append(line);
        Header h = new BufferedHeader(cab);
        headerList.put(h.getName(),h);
      }
      
      line_index++;
    }
    bur.close();
    sr.close();
    
    return headerList;
  }
  
  //???????????
  public static StatusLine createStatusLine(int status,String reason){
    BasicLineParser parser = new BasicLineParser();
    return BasicLineParser.parseStatusLine(HTTP_PROTOCOL+" "+status+" "+reason,parser);
  }
  
  //HTTP????????
  public static HttpResponse createHttpResponse(int status,String reason,HttpEntity entity){
    StatusLine header_sl = HttpUtil.createStatusLine(status,reason);
    HttpResponse hr = new BasicHttpResponse(header_sl);
    hr.setEntity(entity);    
    return hr; 
  }
  
  public static HttpEntity createHttpEntity(String contentType,byte[] entity_buf){
    ByteArrayEntity he = new ByteArrayEntity(entity_buf);
    he.setContentType(contentType);
    return he;
  }
  
  public static String getMimeTypeFromUri(String uri){
    //?????MimeType???
    String ext = MimeTypeMap.getSingleton().getFileExtensionFromUrl(uri);
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    
    //????? "application/octet-stream"
    //ToDo mime-type????????????mime-type???????????????
    mimeType = (mimeType != null)?mimeType:"application/octet-stream";
    Log.d(HttpUtil.class.getName(),"ext:"+ext+" mimeType:"+mimeType);

    return mimeType;
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Http Connection
2.Using HttpGet to get the web response
3.Create Http connection
4.Http connection with
5.HttpGet and DefaultHttpClient
6.Http Post
7.Simple HTTP Request
8.Http Request Class
9.Http Get and Http Post
10.Get Text from HttpResponse
11.Http Request
12.Http Downloader
13.Is Http Url Available
14.Http Retriever
15.Receive Response from HttpURLConnection
16.Print http headers. Useful for debugging.
17.Return the base URL from the given URL. Example: http://foo.org/abc.html -> http://foo.org/
18.Send message with HttpPost
19.Get Http Stream
20.Generic Object Http Loader
21.Http Get and DefaultHttpClient
22.Gets http output from URL
23.Util for Http Get
24.do Http Get Request and return the status code
25.Http Get
26.implements HttpClient
27.Get File From HTTP
28.Make all redirects to go to http in stead of https
29.New Http Client Manager
30.Http Client Manager
31.Multipart Post
32.Get Server Data
33.Yahoo News Crawler
34.Send Packet
35.Read a web page
36.Data Send Utils
37.This class is in charge of synchronizing the events (with due dates) with Google Calendar
38.Update Favicon
39.Converts key-value pair to appropriate signature for Facebook.