Peticion POST a xHarbour

Peticion POST a xHarbour

Postby servicomver » 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();
}
servicomver
 
Posts: 179
Joined: Fri Nov 18, 2005 7:34 pm

Re: Peticion POST a xHarbour

Postby leandro » 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: Select all  Expand view

    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

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1481
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: Peticion POST a xHarbour

Postby leandro » Wed Jun 07, 2023 12:03 pm

Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1481
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: Peticion POST a xHarbour

Postby servicomver » Thu Jun 08, 2023 1:36 am

Muchísimas gracias Leandro por tu respuesta, lo probaré, Saludos
servicomver
 
Posts: 179
Joined: Fri Nov 18, 2005 7:34 pm


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 84 guests