1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class HTTP {
  5.  
  6. private String baseURL;
  7. private String path;
  8. private String fullPath;
  9. private HttpURLConnection http;
  10.  
  11. public HTTP(String url){
  12. this.fullPath=url.indexOf("http://")==0?url:"http://".concat(url);
  13. this.baseURL=getBaseUrl(url);
  14. this.path="";
  15. this.openConnection();
  16. }
  17.  
  18. public void addHeaders(String[] headers,String values){
  19. for(int i=0;i<headers.length;i++);
  20.  
  21. }
  22.  
  23. public void changePath(String path){
  24. this.path=path;
  25. this.fullPath=this.baseURL.concat(path);
  26. this.openConnection();
  27. }
  28.  
  29. public static String getBaseUrl(String url){
  30. String val="http://";
  31. if(!url.contains("http://")) url="http://".concat(url);
  32. String results[]=url.split("/");
  33. if(results.length>=3) return val.concat(results[2]);
  34. return "";
  35. }
  36.  
  37. private void openConnection(){
  38. try {
  39. URL u = new URL(this.fullPath);
  40. this.http = (HttpURLConnection) u.openConnection();
  41. }catch (MalformedURLException ex) {
  42. System.err.println(this.fullPath + " is not a URL I understand");
  43. }catch (IOException ex) {
  44. System.err.println(ex);
  45. }
  46. }
  47.  
  48. public void setHeader(String header,String value){
  49. this.http.setRequestProperty(header, value);
  50. }
  51.  
  52. public void addHeader(String header,String value){
  53. this.http.addRequestProperty(header, value);
  54. }
  55.  
  56. public void printHeaders(){
  57. for (int j = 1; ; j++) {
  58. String header = this.http.getHeaderField(j);
  59. String key = this.http.getHeaderFieldKey(j);
  60. if (header == null || key == null) break;
  61. System.out.println(this.http.getHeaderFieldKey(j) + ": " + header);
  62. }
  63. }
  64.  
  65. public static void main(String[] args) {
  66. HTTP h=new HTTP("http://google.com/agsa");
  67. h.setHeader("Referer", "wwwa.ga.a");
  68. h.printHeaders();
  69. }
  70.  
  71. }