Skip to content

Instantly share code, notes, and snippets.

@kodiyan
Last active August 29, 2015 13:57
Show Gist options
  • Save kodiyan/9840506 to your computer and use it in GitHub Desktop.
Save kodiyan/9840506 to your computer and use it in GitHub Desktop.
Runnig and getting mocky response
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MockyCall {
private final String USER_AGENT;
public MockyCall (String userAgent) {
// super();
this.USER_AGENT = userAgent;
}
public String getUSER_AGENT() {
return USER_AGENT;
}
public void sendPost(String serviceUrl) throws IOException {
long startTime = System.currentTimeMillis();
System.out.println(serviceUrl);
URL obj = new URL(serviceUrl);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + serviceUrl);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
long endTime = System.currentTimeMillis();
System.out.println("\n Total Running Time: " + (endTime - startTime)
+ " milliseconds");
// print result
System.out.println(response.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment