2013-11-27 29 views
0

我創建了一個像這樣的HttpURLConnection對象。如何把自定義的請求位置在java中的HttpURLConnection

URL urlObj = new URL(「http://www.example.com/login」); HttpURLConnection conn; conn = urlObj.openConnection(); conn.setRequestMethod(「GET」);

所以,當我請求,它會是這樣的:

GET http://www.example.com/login HTTP/1.1

,但我想改變http://www.example.com/login只/登錄。 我的意思是這樣的:

GET /登錄HTTP/1.1

有沒有辦法做到這一點?

感謝

PS:我查過使用代理服務器的請求的形式,它看起來像:

GET http://example.com/login HTTP/1.1 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-US,en;q=0.5 
Host: example.com 
Proxy-Connection: keep-alive 

代碼

package test; 

import java.io.*; 
import java.util.*; 
import java.net.*; 
import javax.net.ssl.*; 
import java.security.*; 

public class UBConnection 
{ 

    private List<String> cookies; 
    private final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"; 
    private int fileCounter = 0; 

    public static void main(String[] args) throws Exception 
    { 
     System.setProperty("http.proxySet", "true"); 
     System.setProperty("http.proxyHost", "localhost"); 
     System.setProperty("http.proxyPort", "2121"); 
     UBConnection http = new UBConnection(); 
     http.login("gfhgfh", "fghfh"); 
    } 

    public UBConnection() throws Exception 
    { 
     configSSL(); 
    } 

    public static void configSSL() throws Exception 
    { 
     System.setProperty("https.protocols", "SSLv3"); 
     SSLContext ctx = SSLContext.getInstance("TLS"); 
     ctx.init(new KeyManager[0], new TrustManager[] 
     { 
      new DefaultTrustManager() 
     }, new SecureRandom()); 
     SSLContext.setDefault(ctx); 
     CookieHandler.setDefault(new CookieManager()); 
    } 

    public void login(String username, String password) throws Exception 
    { 
     String loginUrl = "http://www.example.com/login"; 
     CookieHandler.setDefault(new CookieManager()); 
     //String page = new String(send(true, loginUrl, "", true)); 
     String postParams = getFormParams(username, password); 
     byte[] b = send(true, loginUrl, postParams, false); 
    } 

    private byte[] send(boolean isGet, String url, String postParams, boolean isSecure) throws Exception 
    { 
     if (postParams == null) 
     { 
      postParams = ""; 
     } 
     URL urlObj = new URL(url); 
     HttpURLConnection conn; 
     if (isSecure == true) 
     { 
      SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); 
      conn = (HttpsURLConnection) urlObj.openConnection(); 
      ((HttpsURLConnection) conn).setSSLSocketFactory(sslsocketfactory); 
     } 
     else 
     { 
      conn = (HttpURLConnection) urlObj.openConnection(); 
     } 
     if (isGet == true) 
     { 
      conn.setRequestMethod("GET"); 
     } 
     else 
     { 
      conn.setRequestMethod("POST"); 
     } 
     System.out.println("Aum: " + conn); 
     conn.setUseCaches(false); 
     conn.setRequestProperty("Host", urlObj.getHost()); 
     conn.setRequestProperty("User-Agent", USER_AGENT); 
     conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
     conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); 
     conn.setRequestProperty("Referer", url); 
     if (cookies != null) 
     { 
      for (String cookie : this.cookies) 
      { 
       conn.addRequestProperty("Cookie", cookie); 
      } 
     } 
     conn.setRequestProperty("Connection", "keep-alive"); 
     conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
     if (isGet == false) 
     { 
      conn.setRequestProperty("Content-Length", Integer.toString(postParams.length())); 
     } 

     conn.setDoOutput(true); 
     conn.setDoInput(true); 

     if ((isGet == false) && (postParams != null)) 
     { 
      DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); 
      wr.writeBytes(postParams); 
      wr.flush(); 
      wr.close(); 
     } 
     int responseCode = conn.getResponseCode(); 
     System.out.println("\nSending " + (isGet == true ? "GET" : "POST") + " request to URL : " + url); 
     if (isGet == false) 
     { 
      System.out.println("Post parameters : " + postParams); 
     } 
     System.out.println("Response Code : " + responseCode); 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     DataInputStream din = new DataInputStream(conn.getInputStream()); 
     int i = 0; 
     while (i != -1) 
     { 
      i = din.read(); 
      bos.write(i); 
     } 
     byte[] b = bos.toByteArray(); 
     putToFile(new String(b), "file" + fileCounter + ".htm"); 
     fileCounter++; 
     return b; 
    } 

    public String getFormParams(String username, String password) throws UnsupportedEncodingException 
    { 
     return "auth=ldap&url=%5EU&user=" + username + "&pass=" + password + "&submit.x=66&submit.y=23"; 
    } 

    public List<String> getCookies() 
    { 
     return cookies; 
    } 

    public void setCookies(List<String> cookies) 
    { 
     this.cookies = cookies; 
    } 

    public byte[] setPost(String url, String params, boolean https) throws Exception 
    { 
     return send(false, url, params, https); 
    } 

    public byte[] setGet(String url, String params, boolean https) throws Exception 
    { 
     return send(true, url, params, https); 
    } 

    private void putToFile(String data, String filename) throws Exception 
    { 
     System.out.println("Saving to file " + filename); 
     FileWriter fw = new FileWriter(new File(filename)); 
     fw.write(data); 
     fw.close(); 
    } 
} 

代理服務器代碼:

package test; 

import java.io.ByteArrayOutputStream; 
import java.io.DataInputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class SimpleProxy 
{ 

    public static void main(String arg[]) throws Exception 
    { 
     ServerSocket server = new ServerSocket(2121); 
     System.out.println("Server Started on 2121\n--------------------------------------------------------"); 
     while (true) 
     { 
      Socket socket = server.accept(); 
      ClientConnection client = new ClientConnection(socket); 
      client.start(); 
     } 
    } 
} 

class ClientConnection extends Thread 
{ 
    Socket clientCon; 
    private String response = "HTTP/1.1 200 OK\n" + 
"Date: Wed, 27 Nov 2013 04:47:00 GMT\n" + 
"Server: Apache\n" + 
"Pragma: no-cache\n" + 
"Keep-Alive: timeout=2, max=32\n" + 
"Connection: Keep-Alive\n" + 
"Content-Type: text/html; charset=UTF-8"; 

    public ClientConnection(Socket socket) 
    { 
     clientCon = socket; 
    } 

    public void run() 
    { 
     try 
     { 
      DataInputStream din = new DataInputStream(clientCon.getInputStream()); 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      int i = 0; 
      while (din.available() > 0) 
      { 
       i = din.read(); 
       bos.write(i); 
      } 
      String request = new String(bos.toByteArray()); 
      System.out.println(request); 
      System.out.println("--------------------------------------------------------"); 
      clientCon.getOutputStream().write(response.getBytes()); 
      clientCon.close(); 
     } 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 
} 
+0

您的'SimpleProxy'無效。首先,HTTP中的行結束符被定義爲'\ r \ n',而不是'\ n'。其次,你在濫用'available()。'第三,你沒有檢查流結束。 – EJP

+0

好吧,它正在顯示請求字符串。儘管我添加了\ r \ n它會起到同樣的作用。 「濫用」意味着什麼?我如何檢查流結束? – Harikrishnan

回答

1

所以,當我請求,它會是這樣的:

GET http://www.example.com/login HTTP/1.1 

沒有也不會。

但我想將http://www.example.com/login更改爲/ login。我的意思是這樣的:

GET /login HTTP/1.1 

這就是它如何去。

+0

謝謝你的回覆。我通過代理服務器查看了輸出,該代理服務器顯示如GET的輸出http://www.example.com/login HTTP/1.1 – Harikrishnan

+0

我不知道通過代理服務器是什麼意思,我不知道爲什麼你將HTTP回覆作爲請求內容的證據。也許代理正在修改標題。 – EJP

+0

我已添加代碼。代理服務器實際上是一個用java編寫的代碼來打印請求。 – Harikrishnan

2

實際請求w

GET /login HTTP/1.1 
Host: www.example.com 
... 

主機頭將被設置爲www.example.com

URL.openConnection 

將使用原始Socket

建立www.example.com連接如果您的請求通過代理:生病還在使用進行服務器,文件路徑將被設置爲整個URL(在你的案例http://www.example.com/login)。配置使用代理與使用帶有4個參數的URL構造函數的重載形式相同:protocol,host,port,path。

URL url = new URL("http", proxyHost, proxyPort, "http://www.example.com/login"); 

不使用代理服務器:

URL url = new URL("http", "www.example.com", 80, "/login"); 
+0

謝謝你的回覆。但我使用代理服務器檢查過。它就像GET http://www.example.com/login HTTP/1.1 – Harikrishnan

+0

我得到了答覆:

GET http://example.com/login HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Host: example.com Proxy-Connection: keep-alive
Harikrishnan

+0

這是一個答覆,而不是一個請求。這不是如何通過'HttpURLConnection發送請求。「你從哪裏得到它的?上游服務器?代理? – EJP

相關問題