Skip to content

Instantly share code, notes, and snippets.

@kkashyap1707
Last active June 27, 2016 10:16
Show Gist options
  • Save kkashyap1707/2468ddc36b65b6c187fbfa729e3f4d54 to your computer and use it in GitHub Desktop.
Save kkashyap1707/2468ddc36b65b6c187fbfa729e3f4d54 to your computer and use it in GitHub Desktop.
public String apiMethod_POST()
{
String foutput = "";
try
{
URL url = new URL(uri);
System.out.println("API URL : " + url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
if(authToken!=null && authToken.length()>0)
{
conn.setRequestProperty("X-Auth-Token",authToken);
}
System.out.println("JSON sent in input is .........>>>>>>>" + json);
OutputStream os = conn.getOutputStream();
os.write(json.getBytes());
os.flush();
int Responsecode = conn.getResponseCode();
String ResponseCode=String.valueOf(Responsecode);
String ResponseMessage = conn.getResponseMessage();
String LoginContentType = conn.getContentType();
if (conn.getResponseCode() != 200)
{
throw new RuntimeException("Failed : HTTP Error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output ;
while ((output = br.readLine()) != null)
{
foutput = foutput + output;
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return foutput;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment