Request external file
- Print
- PDF
Request external file
- Print
- PDF
Article summary
Did you find this summary helpful?
Thank you for your feedback
Available in Classic and VPC
This document describes examples of using the Request CAPTCHA audio file API.
Request CAPTCHA audio file
This section describes API examples that request a CAPTCHA audio file using an issued CAPTCHA key.
Java
The following is a Java-based sample code for the Request CAPTCHA audio file API.
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class APIExamCaptchaAudio {
public static void main(String[] args) {
String clientId = "YOUR_CLIENT_ID";//Application's client ID value";
try {
String key = "CAPTCHA_KEY"; // Key value received from the https://naveropenapi.apigw.ntruss.com/scaptcha/v1/skey call
String apiURL = "https://naveropenapi.apigw.ntruss.com/scaptcha-bin/v1/scaptcha?key=" + key + "&X-NCP-APIGW-API-KEY-ID" + clientId;
URL url = new URL(apiURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
BufferedReader br;
if(responseCode==200) { // Successful call
InputStream is = con.getInputStream();
int read = 0;
byte[] bytes = new byte[1024];
// Create a file with a random name
String tempname = Long.valueOf(new Date().getTime()).toString();
File f = new File(tempname + ".wav");
f.createNewFile();
OutputStream outputStream = new FileOutputStream(f);
while ((read =is.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
is.close();
} else { // Error occurred
br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = br.readLine()) != null) {
response.append(inputLine);
}
br.close();
System.out.println(response.toString());
}
} catch (Exception e) {
System.out.println(e);
}
}
}
Python
The following is a Python-based sample code for the Request CAPTCHA audio file API.
import os
import sys
import urllib.request
client_id = "YOUR_CLIENT_ID"
key = "YOUR_CAPTCHA_KEY" # CAPTCHA key value
url = "https://naveropenapi.apigw.ntruss.com/scaptcha-bin/v1/scaptcha?key=" + key + "&X-NCP-APIGW-API-KEY-ID=" + client_id;
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
rescode = response.getcode()
if(rescode==200):
print("Save CAPTCHA audio")
response_body = response.read()
with open('captcha.wav', 'wb') as f:
f.write(response_body)
else:
print("Error Code:" + rescode)
PHP
The following is a PHP-based sample code for the Request CAPTCHA audio file API.
<?php
$client_id = "YOUR_CLIENT_ID";
$key = "CAPTCHA_KEY";
$url = "https://naveropenapi.apigw.ntruss.com/scaptcha-bin/v1/scaptcha?key=".$key."&X-NCP-APIGW-API-KEY-ID=".$client_id;
$is_post = false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $is_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, null);
$response = curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "status_code:".$status_code."<br />";
curl_close ($ch);
if($status_code == 200) {
//echo $response;
$fp = fopen("captcha.jpg", "w+");
fwrite($fp, $response);
fclose($fp);
echo "<img src='captcha.jpg'>";
} else {
echo "Error content:".$response;
}
?>
JavaScript
The following is a JavaScript-based sample code for the Request CAPTCHA audio file API.
var express = require('express');
var app = express();
var client_id = 'YOUR_CLIENT_ID';
var fs = require('fs');
app.get('/captcha/wav', function (req, res) {
var api_url = 'https://naveropenapi.apigw.ntruss.com/scaptcha-bin/v1/scaptcha?key=' + req.query.key + req.query.key + "&X-NCP-APIGW-API-KEY-ID=" + client_id;
var request = require('request');
var options = {
url: api_url
};
var writeStream = fs.createWriteStream('./captcha.wav');
var _req = request.get(options).on('response', function(response) {
console.log(response.statusCode) // 200
console.log(response.headers['content-type'])
});
_req.pipe(writeStream); // Output to file
_req.pipe(res); // Output to browser
});
app.listen(3000, function () {
console.log('http://127.0.0.1:3000/captcha/wav?key=캡차키 app listening on port 3000!');
C#
The following is a C#-based sample code for the Request CAPTCHA audio file API.
using System;
using System.Net;
using System.Text;
using System.IO;
namespace NaverAPI_Guide
{
public class APIExamCaptchaAudio
{
static void Main(string[] args)
{
string key = "KEY-INPUT"; // Key value received from the https://naveropenapi.apigw.ntruss.com/scaptcha/v1/skey call
string url = "https://naveropenapi.apigw.ntruss.com/scaptcha-bin/v1/scaptcha?key=" + key + "&X-NCP-APIGW-API-KEY-ID=YOUR-CLIENT-ID";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string status = response.StatusCode.ToString();
Console.WriteLine("status="+ status);
using (Stream output = File.OpenWrite("c:/captcha.wav"))
using (Stream input = response.GetResponseStream())
{
input.CopyTo(output);
}
Console.WriteLine("c:/captcha.wav was created");
}
}
}
Was this article helpful?