我正在尝试在此页面http://www2.gcitrading.com/quotes/converter.asp上发出帖子请求,但它不起作用..在我的帖子请求之后我仍然得到相同的页面(没有结果)。
当我使用浏览器时,点击转换后页面变为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 对其进行了测试。