我想在此頁面上發佈一個請求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對其進行了測試。
嘗試使用程序,如[提琴手(http://www.fiddler2.com/fiddler2/)看您要發送的請求。 – rmtheis 2012-07-13 08:44:26
是的..我已經想到了,但問題是我在Mac上。 – tambalolo 2012-07-13 08:55:14
[HttpFox](https://addons.mozilla.org/en-US/firefox/addon/httpfox/)? – rmtheis 2012-07-13 08:57:15