2015-06-14 46 views
0

我使用HttpUrlConnection來連接一個Web服務,我有一個窗體和一個按鈕。Post with HTTPUrlConnection

源代碼的部分是:

<form action="main.php4?page=markt3art=UIN=484504321483db0fc35d1e8e660b99c8&x=&y=filled=1" method="POST"> 
     <input type="hidden" value="" name="p"></input> 
     <input type="hidden" value="1755219" name="buy2f8d0"></input> 
     <input type="hidden" value="1755219" name="buy"></input> 
     <input type="text" value="323" size="9" maxlength="16" name="teilkauf"></input> 
     <img width="1" hspace="20" height="1" src="pics/leer.gif"></img> 
     <input class="send" type="submit" value=" buy "></input> 
    </form> 

我在做什麼是:(www是網站的http:// ...)

  URL url = new URL(www); 
      HttpURLConnection conexion = (HttpURLConnection)url.openConnection(); 
      String forSending = "1"; 
      String charset = "UTF-8"; 

      String stringToSend = URLEncoder.encode(forSending, charset); 

      conexion.setDoOutput(true); 
      conexion.setRequestMethod("POST"); 
      OutputStreamWriter out = new OutputStreamWriter(
      conexion.getOutputStream()); 

      out.write("teilkauf=" + stringToSend); 
      out.flush(); 
      out.close(); 

但它不」工作。任何幫助?謝謝。

回答

0

您必須向表單的操作發出POST請求,而不是表單所在的網站。

表單就像後端服務器的前端,當您單擊提交瀏覽器時,會爲該操作構建請求。的

因此,而不是發佈到窗體的位置POST到main.php4?頁= markt3art = UIN = 484504321483db0fc35d1e8e660b99c8 & X = & Y =充滿= 1

+0

難道我只有通過HTTP來改變WWW(網站) ://...main.php4頁= markt3art = UIN = 484504321483db0fc35d1e8e660b99c8&X =&Y =填充= 1?因爲它不起作用。 (對不起我的英語不好)。 – Kakuti

+0

您可能需要在每次要發佈該數據時都要獲取該UIN令牌。 –