FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Peticion POST a xHarbour
Posts: 198
Joined: Fri Nov 18, 2005 07:34 PM
Peticion POST a xHarbour
Posted: Mon Jun 05, 2023 11:15 PM

Hola, es posible realizar esta conexion con xharbour ?,

String mensaje = "Mensaje de prueba";

String numero = "5551234567";

String api = "TU APIKEY AQUI";

String urlRequest="https://api.smsmasivos.com.mx/v1/sms/send";

String urlParametros = "apikey=" + URLEncoder.encode(api, "UTF-8") +

"&mensaje=" + URLEncoder.encode(mensaje, "UTF-8") +

"&numcelular=" + URLEncoder.encode(numero, "UTF-8") +

"&numregion=" + URLEncoder.encode("52", "UTF-8");

URL url;

HttpsURLConnection conexion = null;

try {

//-----------------------CREAR CONEXION----------------------------

url = new URL(urlRequest);

conexion = (HttpsURLConnection)url.openConnection();

conexion.addRequestProperty("User-Agent", "");

conexion.setRequestMethod("GET");

conexion.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

conexion.setRequestProperty("Content-Length", ", " + Integer.toString(urlParametros.getBytes().length));

conexion.setRequestProperty("Content-Language", "en-US");

conexion.setUseCaches (false);

conexion.setDoInput(true);

conexion.setDoOutput(true);

//-----------------------------------------------------------------



//--------------------ENVIAR PETICION------------------------------

DataOutputStream wr = new DataOutputStream ( conexion.getOutputStream() );

wr.writeBytes(urlParametros);

wr.flush();

wr.close();

//-----------------------------------------------------------------



//-----------------------OBTENER RESPUESTA-------------------------

InputStream is = conexion.getInputStream();

BufferedReader rd = new BufferedReader(new InputStreamReader(is,"UTF-8"));

String line;

StringBuffer responsee = new StringBuffer();

while((line = rd.readLine()) != null) {

  responsee.append(line);

  responsee.append('\r');

}

rd.close();

String respuesta = responsee.toString();

//-----------------------------------------------------------------



//-------------Decodificar JSON con libreria org.json--------------

JSONObject jsonObject = new JSONObject(respuesta);



if(jsonObject.getString("estatus").equals("ok")){

    out.print(jsonObject.getString("mensaje")+"");

    out.print("Referencia: "+jsonObject.getString("referencia")+"");

}else{

    out.print(jsonObject.getString("mensaje")+"");

    out.print("Codigo error: "+jsonObject.getString("codigo")+"");

}

//-----------------------------------------------------------------

}catch (Exception e) {

out.print(e);

}finally {

if(conexion != null) conexion.disconnect();

}

Posts: 1816
Joined: Wed Oct 26, 2005 02:49 PM
Re: Peticion POST a xHarbour
Posted: Wed Jun 07, 2023 11:58 AM
Hola buenos d铆as como estas?

Pues no he probado este c贸digo, pero creo que podr铆a funcionar.

Lo otro que hay que aclarar es que en el encabezado dice que petici贸n POST, pero en el c贸digo es por GET.
Code (fw): Select all Collapse
聽聽 聽mensaje := "Mensaje de prueba"
聽 聽 numero := "5551234567"
聽 聽 api := "TU APIKEY AQUI"
聽 聽 urlParametros := "apikey="+api+"&mensaje="+mensaje+"&numcelular="+numero+"&numregion=52"
聽聽 聽cUrl := "https://api.smsmasivos.com.mx/v1/sms/send"
聽 聽 
聽 聽 ohttp := CreateObject( "MSXML2.XMLHTTP" )
聽 聽 ohttp:Open( "GET" ,cUrl,.F.)
聽 聽 ohttp:SetRequestHeader("content-type", "application/x-www-form-urlencoded" )
聽 聽 oHttp:SetRequestHeader("Content-Length", ", "+urlParametros)
聽 聽 ohttp:SetRequestHeader("Content-Language", "en-US " )
聽 聽 oHttp:SetRequestHeader("cache-control", "no-cache")
聽 聽 TRY
聽 聽 聽 ohttp:Send()
聽 聽 CATCH
聽 聽 聽 聽 msginfo("No se pudo enviar petici贸n")
聽 聽 END
聽 聽 
聽 聽 response:=ohttp:responseText
聽 聽 memoedit(response) //Para comprobar la respuesta
聽 聽 
聽 聽 aHasRes := hash()
聽 聽 hb_jsondecode(response ,@aHasRes) //Convertimos a hash la respuesta 
聽 聽 
聽 聽 if valtype(aHasRes)="U"
聽 聽 聽 聽 msginfo("Respuesta del servidor invalida")
聽 聽 else
聽 聽 聽 聽 //....aqui el codigo para actualizar las bases de datos
聽 聽 endif
Saludos
LEANDRO AREVALO
Bogot谩 (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Turbo Incremental Link64 6.98 Embarcadero 7.70 ] [ FiveWin 25.01 ] [ xHarbour 64 bits) ]
Posts: 1816
Joined: Wed Oct 26, 2005 02:49 PM
Re: Peticion POST a xHarbour
Posted: Wed Jun 07, 2023 12:03 PM
Saludos
LEANDRO AREVALO
Bogot谩 (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Turbo Incremental Link64 6.98 Embarcadero 7.70 ] [ FiveWin 25.01 ] [ xHarbour 64 bits) ]
Posts: 198
Joined: Fri Nov 18, 2005 07:34 PM
Re: Peticion POST a xHarbour
Posted: Thu Jun 08, 2023 01:36 AM

Much铆simas gracias Leandro por tu respuesta, lo probar茅, Saludos

Continue the discussion