Note that there are some explanatory texts on larger screens.

plurals
  1. POSockets: Simple POST request not working
    text
    copied!<p>I am trying to make a post request on this page <a href="http://www2.gcitrading.com/quotes/converter.asp" rel="nofollow">http://www2.gcitrading.com/quotes/converter.asp</a>, but its not working..I still get the same page after my post request (without the result).</p> <p>When I use the browser, after I click convert the page turns to <a href="http://www2.gcitrading.com/quotes/converter.asp?lang=" rel="nofollow">http://www2.gcitrading.com/quotes/converter.asp?lang=</a> Im really confused with this one. How can I make this work?</p> <p>Here is my code:</p> <pre><code>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 &lt; 1) break; res += new String(readBuffer, 0, readSize); if(res.contains("&lt;/html&gt;")) 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&amp;form_from_currency=DZD&amp;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 &lt; 1) break; res += new String(readBuffer, 0, readSize); if(res.contains("&lt;/html&gt;")) break; } System.out.println(res); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } </code></pre> <p>Thanks. Btw, I need to achieve this using c/c++ sockets, but I tested it using java first.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload