java中如何从http header中获取参数Request

发布于 2024-11-02 23:20:25 字数 1819 浏览 3 评论 0原文

我想创建一个代理,方法 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文