Page 1 of 1

Peticion POST a xHarbour

PostPosted: Mon Jun 05, 2023 11:15 pm
by servicomver
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();
}

Re: Peticion POST a xHarbour

PostPosted: Wed Jun 07, 2023 11:58 am
by leandro
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

 

Re: Peticion POST a xHarbour

PostPosted: Wed Jun 07, 2023 12:03 pm
by leandro

Re: Peticion POST a xHarbour

PostPosted: Thu Jun 08, 2023 1:36 am
by servicomver
Muchísimas gracias Leandro por tu respuesta, lo probaré, Saludos