我想创建一个代理,方法GET工作正常,但POST没有,因为我无法从头http中获取参数将其发送到服务器:
我得到的例子:
POST http://site/index.php HTTP/1.1
Host: host
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; fr; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Referer: http://site/index.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 112
之后我应该有参数:
Login=toto&Password=motdepasse&submit=envoyer
但我无法在追踪中看到它
我用来获取跟踪的代码是:
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while((inputLine = in.readLine()) != null){
try{
ligne+=inputLine+"\n";
StringTokenizer tok = new StringTokenizer(inputLine);
tok.nextToken();
}catch(Exception e){
break;
// System.out.println("ERROR :" + e.getMessage());
}
}
public static String excutePost(String targetURL, String urlParameters)
{
//Create connection
URL url = new URL(targetURL);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","application/x-www-form- urlencoded");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
}