如何使用Delphi代码

时间:2017-06-19 13:16:06

标签: http delphi sms using

我使用Indy10.6.2组件进行了3次尝试,其中没有一个显示任何错误,但代码无法发送短信。请帮我通过Delphi代码发送短信。

尝试1

const
    URL = 'https://api.bulksmsgateway.in/send/?        username=****&hash=****&sender=TXTLCL&numbers=9198........&message=HISUNDAR';
    //URL = 'https://api.textlocal.in/send/?    username=*****&hash=******&sender=TXTLCL&numbers=9198...&message=HISUNDAR';
    ResponseSize = 1024;
var
    hSession, hURL: HInternet;
    Request: String;
    ResponseLength: Cardinal;
begin
    hSession := InternetOpen('TEST', INTERNET_OPEN_TYPE_PRECONFIG, nil,     nil, 0);
    try
        Request := Format(URL,        [Username,Password,Sender,Numbers,HttpEncode(Message1)]);
        hURL := InternetOpenURL(hSession, PChar(Request), nil, 0,0,0);
        try
            SetLength(Result, ResponseSize);
            InternetReadFile(hURL, PChar(Result), ResponseSize,         ResponseLength);
            SetLength(Result, ResponseLength);
        finally
            InternetCloseHandle(hURL)
        end;
        showmessage(result);
    finally
        InternetCloseHandle(hSession)
  end

尝试2

var
    http : TIdHTTP;
    IdSSL : TIdSSLIOHandlerSocketOpenSSL;
begin
    http := TIdHTTP.Create(nil);
    IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    try
        Http.ReadTimeout := 30000;
        Http.IOHandler := IdSSL;
        IdSSL.SSLOptions.Method := sslvTLSv1;
        Http.Request.BasicAuthentication := True;
        // IdSSL.SSLOptions.Method := sslvTLSv1;
        //IdSSL.SSLOptions.Method := sslvTLSv1;
        // http.Get('https://www.smsgatewayhub.com/api/mt/SendSMS?        APIKey=B215dPone0yVIJU2QDH&senderid=TESTIN&channel=2&DCS=0&flashsms=0&number=        9195.....&text=test message&route=1');
        http.Get('http://login.bulksmsgateway.in/sendmessage.php?            user=****&password=****&mobile=95661....&message=Good         Morning&sender=PRAPUS&type=3 ');
    finally
        http.Free;
    end;

尝试3

var
    lHTTP: TIdHTTP;
    lParamList: TStringList;
    lResult: String;
    IdSSL : TIdSSLIOHandlerSocketOpenSSL;
begin
    lParamList := TStringList.Create;
    lParamList.Add('username=****');                                    
    lParamList.Add('password=****');                                
    lParamList.Add('msgtext=Hello World');                                  
    lParamList.Add('originator=TestAccount');                               
    lParamList.Add('phone=+9195....');                                      

    lParamList.Add('showDLR=0');                                                


    lParamList.Add('charset=0');                                                
    lParamList.Add('msgtype=');                                                 
    lParamList.Add('provider=bulksmsgateway.in');                       

    lHTTP := TIdHTTP.Create(nil);                                               
    try

        lResult :=     lHTTP.Post('http://login.bulksmsgateway.in/sendmessage.php?', lParamList);   
        //WriteLn(lResult);                                                     
        // Readln;
    finally
        FreeAndNil(lHTTP);                                              
        FreeAndNil(lParamList);
    end;

1 个答案:

答案 0 :(得分:3)

您使用错误的HTTP方法将错误的参数发送到错误的网址。根据{{​​3}}上的代码示例,您需要使用具有正确URL和参数的HTTP POST。请按照在线示例进行操作。

尝试更像这样的东西:

var
  lHTTP: TIdHTTP;
  lParamList: TStringList;
  lResult: String;
  IdSSL : TIdSSLIOHandlerSocketOpenSSL;
begin
  lParamList := TStringList.Create;
  try
    lParamList.Add('user=****');
    lParamList.Add('password=****' );
    lParamList.Add('message=Hello World');
    lParamList.Add('sender=TestAccount');
    lParamList.Add('mobile=+9195....');
    lParamList.Add('type=1'); // or 3

    lHTTP := TIdHTTP.Create(nil);
    try
      // note: if you are using an up-to-date version of Indy, 
      // assigning the IOHandler is optional:
      //
      // http://www.indyproject.org/sockets/blogs/ChangeLog/20141222.aspx
      //
      lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
      try
        lResult := lHTTP.Post('https://www.bulksmsgateway.in/sendmessage.php', lParamList);
        // WriteLn(lResult);
        // Readln;
      except
        on E: Exception do begin
          //WriteLn('Error: ', e.Message);
        end;
      end;
    finally
      FreeAndNil(lHTTP);
    end;
  finally
    FreeAndNil(lParamList);
  end;
end;

如果您想通过Bulk SMS Gateway website发送,则可以选择使用HTTP GET或XML over HTTP POST

var
  lHTTP: TIdHTTP;
  lParamList, lResult: String;
  IdSSL : TIdSSLIOHandlerSocketOpenSSL;
begin
  lParamList := Format('APIKey=%s&senderid=%s&channel=2&DCS=8&flashsms=0&number=%s&text=%s&route=1',
    [
    '****', 
    'TestAccount',
    '9195....',
    'Hello World'
    ]
  );

  lHTTP := TIdHTTP.Create(nil);
  try
    lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
    try
      lResult := lHTTP.Get('https://www.smsgatewayhub.com/api/mt/SendSMS?' + lParamList);
      // WriteLn(lResult);
      // Readln;
    except
      on E: Exception do begin
        //WriteLn('Error: ', e.Message);
      end;
    end;
  finally
    FreeAndNil(lHTTP);
  end;
end;

var
  lHTTP: TIdHTTP;
  lParamList: TStringStream;
  lResult: String;
  IdSSL : TIdSSLIOHandlerSocketOpenSSL;
begin
  lParamList := TStringStream.Create(
    '<SmsQueue>' +
      '<Account>' +
        '<User>****</User>' +
        '<Password>****</Password>' +
        '<SenderId>TestAccount</SenderId>' +
        '<Channel>1</Channel>' +
        '<DCS>0</DCS>' +
        '<FlashSms>0</FlashSms>' +
        '<Route>1</Route>' +
      '</Account>' +
      '<Messages>' +
        '<Message>' +
          '<Number>9195....</Number>' +
          '<Text>Hello World</Text>' +
        '</Message>' +
      '</Messages>' +
    '</SmsQueue>',
    TEncoding.UTF8);
  try
    lHTTP := TIdHTTP.Create(nil);
    try
      lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
      try
        lHTTP.Request.ContentType := 'text/xml';
        lHTTP.Request.Charset := 'utf-8';
        lResult := lHTTP.Post('https://www.smsgatewayhub.com/RestAPI/MT.svc/mt', lParamList);
        // WriteLn(lResult);
        // Readln;
      except
        on E: Exception do begin
          //WriteLn('Error: ', e.Message);
        end;
      end;
    finally
      FreeAndNil(lHTTP);
    end;
  finally
    FreeAndNil(lParams);
  end;
end;