Skip to main content

Posts

Showing posts with the label http

Implementing OAuth2 consumer in C#

Recently I worked on a project that involved integrating with social networking and jobs websites like Elance. These days almost all major services that allow applications to access users' data, perform authorization/authentication using OAuth1.0 or OAuth2. OAuth2 compared to OAuth1 is very easy to implement. OAuth1 involves generating nonce,timestamp,signaturebase and signing the request with any algorithm like HMAC-SHA1 and appending data to query string of URL and passing in Authorization header of HTTP. OAuth2 removed all these requirements. Following is OAuth2 process in a nutshell 1) Redirect user to Authorization url passing client_id and redirect_url in query parameters 2) If user authenticates successfully through service provider it will redirect user to the redirect_url passed with authorization access code in query parameters. 3) After getting authorization access code you exchange this to receive access token. You make a POST HTTP request to a URL passing client_id ...

HTTP POST request in Java

In one blog post I wrote about how we can make HTTP requests and receive response in Java. That post was only about sending GET request. This time we will learn how to make POST (submitting form etc) request in Java.  This post assumes that you are familiar with  HttpURLConnection  found in  java.net  package. If you are not please read this blog post http://random-stuff-mine.blogspot.com/2013/06/http-request-and-response-java.html HTTP POST request Lets say you entered username/password in a website and pressed "Login" button. Most probably the web browser makes a HTTP POST request containing your username/password. A simple POST request would look like this POST /path/to/script HTTP/1.1 User-Agent: RemzyHttpBot Content-Type: application/x-www-form-urlencoded Content-Length: 32   username=rameez&password=test In the above request you see that username and password are sent inside the request body and content-type of request is set to appl...

HTTP requests and response in Java

Java has excellent support of HTTP client side. In this blog post we will have a look at how to make HTTP request in Java and download the response , saving it in a buffer. Java implements a simple HTTP client in a class java.net.HttpURLConnection . So our first step is to make an instance of java.net.HttpURLConnection. To do this we need java.net.URL class. We will use its constructor which takes url in a form of string as a parameter and then call its openConnection method casting the returned object to HttpURLConnection. Here is how it is done String strUrl="http://google.com"; URL url=new URL(strUrl); HttpURLConnection httpConn=(HttpURLConnection)url.openConnection(); We now have HttpURLConnection object to use. Please note right now no request has been sent to the server (in our case google.com). To complete the process of sending actual request and start getting response we have to use getInputStream  method of HttpURLConnection class. Actually getInputStr...