2012-07-13 42 views
0

我想在此頁面上發佈一個請求http://www2.gcitrading.com/quotes/converter.asp, 但它不能正常工作..我仍然在我的發佈請求(沒有結果)後得到相同的頁面。套接字:簡單的POST請求不起作用

當我使用瀏覽器,我點擊轉換後,頁面變成http://www2.gcitrading.com/quotes/converter.asp?lang=我真的很困惑這一個。我該如何做這項工作?

這裏是我的代碼:

public static void main(String[] args) { 
    Socket sock = new Socket(); 
    InputStream in; 
    OutputStream out; 
    byte[] readBuffer = new byte[4096]; 

    String res = ""; 
     try { 
     sock.connect(new InetSocketAddress("www2.gcitrading.com", 80)); 

     in = sock.getInputStream(); 
     out = sock.getOutputStream(); 

     out.write(new String("GET /quotes/converter.asp HTTP/1.1\r\n").getBytes()); 
     out.write(new String("Host: www2.gcitrading.com\r\n\r\n").getBytes()); 

     while(true) { 
      int readSize = in.read(readBuffer); 
      if(readSize < 1) 
       break; 
      res += new String(readBuffer, 0, readSize); 
      if(res.contains("</html>")) 
       break; 
     } 

     String cookie = res.substring(res.indexOf("kie:") + 5,res.indexOf("path=/")+6); 
     System.out.println("SHow cookie - " + cookie); 

     String convert_this = URLEncoder.encode("form_amount=1&form_from_currency=DZD&form_to_currency=USD", "UTF-8"); 

     out.write(new String("POST /quotes/converter.asp?lang= HTTP/1.1\r\n").getBytes()); 
     out.write(new String("Host: www2.gcitrading.com\r\n").getBytes()); 

     out.write(new String("Content-Length: " + convert_this.length() + "\r\n").getBytes()); 
     out.write(new String("Content-Type: application/x-www-form-urlencoded\r\n").getBytes()); 
     out.write(new String("Cookie: " + cookie +"\r\n").getBytes()); 
     out.write(new String("\r\n").getBytes()); 
     out.write(convert_this.getBytes()); 

     readBuffer = new byte[4096]; 
     res = ""; 

     while(true) { 
      int readSize = in.read(readBuffer); 
      if(readSize < 1) 
       break; 
      res += new String(readBuffer, 0, readSize); 
      if(res.contains("</html>")) 
       break; 
     } 

     System.out.println(res); 


     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
} 

感謝。順便說一句,我需要使用c/C++套接字來實現這一點,但我首先使用java對其進行了測試。

+0

嘗試使用程序,如[提琴手(http://www.fiddler2.com/fiddler2/)看您要發送的請求。 – rmtheis 2012-07-13 08:44:26

+0

是的..我已經想到了,但問題是我在Mac上。 – tambalolo 2012-07-13 08:55:14

+0

[HttpFox](https://addons.mozilla.org/en-US/firefox/addon/httpfox/)? – rmtheis 2012-07-13 08:57:15

回答

0

已解決。我用查爾斯,這是一個網絡調試工具,並發現我的帖子請求缺乏。我簡單地添加在我的崗位要求:convert_it=true

0

嘗試這樣:

DataOutputStream dataOut = new DataOutputStream(sock.getOutputStream()); 
dataOut.writeUTF("[Your String here]"); 

此外,您應該使用更高級別的網址,而不是插座。

+0

nah..still相同 – tambalolo 2012-07-13 06:31:53

+0

我需要在c/C++上這樣做,這就是爲什麼即時通訊使用套接字\ – tambalolo 2012-07-13 06:33:15

1

我試試這個它的工作

String convert_this = URLEncoder.encode("form_amount", "UTF-8")+ "=" + URLEncoder.encode("1", "UTF-8"); 
convert_this += "&" + URLEncoder.encode("form_from_currency", "UTF-8")+ "=" + URLEncoder.encode("DZD", "UTF-8"); 
convert_this += "&" + URLEncoder.encode("form_to_currency", "UTF-8")+ "=" + URLEncoder.encode("USD", "UTF-8");