Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 套接字:简单POST请求不起作用_Java_Sockets_Http Post - Fatal编程技术网

Java 套接字:简单POST请求不起作用

Java 套接字:简单POST请求不起作用,java,sockets,http-post,Java,Sockets,Http Post,我想在这页上发个帖子, 但是它不起作用..在我的post请求之后,我仍然得到相同的页面(没有结果) 当我使用浏览器时,单击“转换”后,页面会变成“我真的很困惑这个”。我怎样才能做到这一点 这是我的密码: public static void main(String[] args) { Socket sock = new Socket(); InputStream in; OutputStream out; byte[] readBuffer = new byte[

我想在这页上发个帖子, 但是它不起作用..在我的post请求之后,我仍然得到相同的页面(没有结果)

当我使用浏览器时,单击“转换”后,页面会变成“我真的很困惑这个”。我怎样才能做到这一点

这是我的密码:

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();
        }
}
publicstaticvoidmain(字符串[]args){
插座插座=新插座();
输入流输入;
输出流输出;
字节[]读缓冲区=新字节[4096];
字符串res=“”;
试一试{
connect(新的InetSocketAddress(“wwww2.gcitrading.com”,80));
in=sock.getInputStream();
out=sock.getOutputStream();
write(新字符串(“GET/quotes/converter.asp HTTP/1.1\r\n”).getBytes();
write(新字符串(“主机:www2.gcitrading.com\r\n\r\n”).getBytes();
while(true){
int readSize=in.read(readBuffer);
如果(readSize<1)
打破
res+=新字符串(readBuffer,0,readSize);
if(res.contains(“”)
打破
}
字符串cookie=res.substring(res.indexOf(“kie:”)+5,res.indexOf(“path=/”)+6);
System.out.println(“显示cookie-”+cookie);
字符串convert_this=urlcoder.encode(“form_amount=1,form_from_currency=DZD,form_to_currency=USD,UTF-8”);
write(新字符串(“POST/quotes/converter.asp?lang=HTTP/1.1\r\n”).getBytes();
write(新字符串(“主机:www2.gcitrading.com\r\n”).getBytes();
write(新字符串(“内容长度:“+convert\u this.Length()+”\r\n”).getBytes());
write(新字符串(“内容类型:application/x-www-form-urlencoded\r\n”).getBytes();
out.write(新字符串(“Cookie:+Cookie+”\r\n”).getBytes();
out.write(新字符串(“\r\n”).getBytes();
out.write(convert_this.getBytes());
readBuffer=新字节[4096];
res=“”;
while(true){
int readSize=in.read(readBuffer);
如果(readSize<1)
打破
res+=新字符串(readBuffer,0,readSize);
if(res.contains(“”)
打破
}
系统输出打印项次(res);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}

谢谢。顺便说一句,我需要使用c/c++套接字来实现这一点,但我首先使用java进行了测试。

尝试以下方法:

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

此外,您应该使用更高级别的URL,而不是套接字。

问题解决了。我使用了Charles,这是一个web调试工具,发现我的post请求不足。我只是在我的发帖请求中添加了:
convert\u it=true

我尝试了这项工作

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

我需要在c/c++上执行此操作,这就是为什么我使用套接字\尝试使用类似这样的程序来查看您发送的请求。是的..我已经想到了,但问题是我在mac上。?好的..我将检查此firefox加载项是的!!问题解决了。我的请求中缺少一些东西,我通过使用一个名为Charles的web调试代理工具找到了答案。顺便说一下,谢谢你的建议。