gpt4 book ai didi

java - 套接字:简单的 POST 请求不起作用

转载 作者:行者123 更新时间:2023-12-01 15:17:11 25 4
gpt4 key购买 nike

我正在尝试在此页面上发出发布请求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 对其进行了测试。

最佳答案

我尝试了一下,成功了

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");

关于java - 套接字:简单的 POST 请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11464882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com