我創建了一個像這樣的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();
}
}
}
您的'SimpleProxy'無效。首先,HTTP中的行結束符被定義爲'\ r \ n',而不是'\ n'。其次,你在濫用'available()。'第三,你沒有檢查流結束。 – EJP
好吧,它正在顯示請求字符串。儘管我添加了\ r \ n它會起到同樣的作用。 「濫用」意味着什麼?我如何檢查流結束? – Harikrishnan