java中如何从http header中获取参数Request
我想创建一个代理,方法 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 ();
}
i want to create a proxy ,the method GET work fine ,but POST no ,cause i can't get the parameter from the header http to send it to the server :
example of what i get:
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
after that i should have the parameters:
Login=toto&Password=motdepasse&submit=envoyer
but i cant see it on the trace
the code that i use to get th trace is :
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 ();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论