Showing posts with label http. Show all posts
Showing posts with label http. Show all posts

HTTP on the command line

OpenSSL s_client


Once connected using c_client -connect, simply type HTTP on the command line to GET/PUT/POST data.

For example:

$ openssl s_client -connect host:port -cert client.pem -key clientkey.pem -CAfile cacerts.pem
Enter pass phrase for clientkey.pem: ********

[...]

PUT /api/rest/version/9/TPRGMPITEST/3DSecureId/12345678 HTTP/1.1
Host: [...]
User-Agent: [...]
Accept: application/json
Content-Type: application/json
Content-Length: 367
Connection: close

{"3DSecure":{ ... }}





This works for JSON data. If you need to POST a html form, set the Content-Type header to "application/x-www-form-urlencoded", e.g. something like:

POST /bin/login HTTP/1.1
Host: 127.0.0.1:8000
Accept: image/gif, image/jpeg, */*
Referer: http://127.0.0.1:8000/login.html
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Content-Length: 37
Connection: Keep-Alive
Cache-Control: no-cache
   
User=Peter+Lee&pw=123456&action=login



Other Options


For examples of using telnet to send raw HTTP commands:
http://wiki.apache.org/couchdb/CouchIn15Minutes
http://www.ntu.edu.sg/home/ehchua/programming/webprogramming/HTTP_Basics.html

Simple HTTP Request

Simple code sample that executes a HTTP GET using apache HttpClient

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public String executeGet(String url) throws ClientProtocolException, IOException {
  
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response = httpClient.execute(httpGet);
  
    HttpEntity entity = response.getEntity();
    return EntityUtils.toString(entity);
}