2009-12-11 27 views
0

這可能是個問題,但我解決不了。 我需要自動填寫Web窗體上的一些字段,這是從服務器提供的。 我使用Apache的HttpClient讓更多的方便了我的生活) 現在,可以考慮一下我的步驟acheive目的:Cookie ... Http POST無法正常工作,爲什麼?

    1.我沒有證書http://trac.edgewall.org/所以我下載此軟件並在本地安裝並完成後,我將不得不創建NewTicket。
    2.我本地使用Trac時沒有任何SSL(SSL隧道)。 (將我的程序更改爲可以使用HTTPS並不困難)。
    3.現在,我可以驗證並執行GET請求,但我無法執行POST請求

    4.例如:我執行GET請求 http://localhost:8000/tracenvir/newticket。 這(〜/ newticket)頁面看起來如下: http://s04.radikal.ru/i177/0912/cb/d43971cebc02.png 而作爲迴應,我有:
    (的一部分) 「輸入型=」 文本」 ID = 「現場總結」 NAME = 「field_summary」 大小= 「70」 「

    」的textarea ID =「 現場描述」 名稱= 「field_description」 類= 「wikitext的」 行= 「10」 的cols = 「68」/ textarea的「」
    5.所以,我寫這個:

     int status = 0; 
     int cookLength=0; 
     Cookie[] cookies = null; 

     HttpClient client = new HttpClient(); 
     client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); 
     HttpState initialState = new HttpState(); 
     client.setState(initialState); 

     //**********// 
     //**Log in**// 
     //**********// 

     GetMethod login = new GetMethod("http://localhost:8000/tracenvir/login"); 
     client.getState().setCredentials(AuthScope.ANY, 
       new UsernamePasswordCredentials("rauch", "qwert")); 
     login.setDoAuthentication(true); 
     System.setProperty("javax.net.ssl.trustStore", "/home/rauch/NetBeansProjects/jssecacerts"); 

     try { 
      status = client.executeMethod(login); 

     System.out.println("response code = "+status); 
     cookies = client.getState().getCookies(); 
     cookLength = cookies.length; 

     for(int i=0;i (less than) cookLength;i++) { 
      System.out.println(cookies[i].toString()); 
     } 
     login.releaseConnection(); 
     } catch(IOException ex) { 
      ex.printStackTrace(); 
     } 

     //*********************// 
     //**Create New Ticket**// 
     //*********************// 

     PostMethod post = new PostMethod("http://localhost:8000/tracenvir/newticket"); 

     NameValuePair[] data = { 
      new NameValuePair("field-summary","second error"), 
      new NameValuePair("field-descryption","Some stupid descryption..."), 
      new NameValuePair("field-type","defect"), 
      new NameValuePair("field-priority","major"), 
      new NameValuePair("field-version","1.0"), 
      new NameValuePair("field-owner","moscow server"), 
      new NameValuePair("submit","create ticket"), 
     }; 


     //post.setRequestBody(data); 
     post.addParameters(data); 
     post.addRequestHeader("Referer","http://localhost:8000/tracenvir/login"); 
     for(int i=0;i (less than) cookLength;i++) { 
      initialState.addCookie(cookies[i]); 
     } 
     client.setState(initialState); 


     try { 
      status = client.executeMethod(post); 

     System.out.println("response code = "+status); 

     byte[] buf = new byte[10]; 
     int r=0; 
     BufferedInputStream is = new BufferedInputStream(post.getResponseBodyAsStream()); 

     while((r = is.read(buf)) > 0) { 
      System.out.write(buf, 0, r); 
     } 
     post.releaseConnection(); 
     } catch(IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 

而且我有這樣的:

    400錯誤:錯誤的請求
    缺失或無效的形式標記。你有cookies嗎?

怎麼了?

    作爲上GET請求響應我得到這樣的:
      響應代碼= 200 trac_auth = 38144ec2830678183afebf0b14c51721
      trac_form_token = e9648f17987551b8f97e1953

大概我NEDD更改此:

    client.getParams ().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

回答

1

http://www.google.com/support/toolbar/bin/answer.py?hl=en&answer=47972

我覺得上面的鏈接將幫助ü在這個題目。其提供的視頻也..我希望您的查詢就會迎刃而解

http://www.formautofill.com/

這個網站會給你一個軟件,該軟件將提供自動填表。由微軟提供。

+0

否...我必須用POST方法填寫表單。 非常感謝,但對我來說沒用... – rauch 2009-12-11 19:15:30

+0

好的。我會盡力得到答案。因爲。這個我以爲我做到了。 1年以前。當我在一個工具欄中開發時。這是在這個基地。 ..好吧 – sikender 2009-12-11 20:39:02

+0

哦。對不起。事情。沒想到。 – sikender 2009-12-11 20:39:35