gpt4 book ai didi

未从 Java Web 应用程序接收短信

转载 作者:行者123 更新时间:2023-12-01 09:07:37 25 4
gpt4 key购买 nike

我编写了java代码,用于使用短信网关发送短信。 JAVA OpenSMPP API用于实现发送SMPP请求的逻辑。我需要以下信息用于连接到短信网关并用于发送短信:

SMS_GATEWAY_USERNAME     906o2portt02       
SMS_GATEWAY_PORT 9205 S
SMS_GATEWAY_IP_2 34.22.91.166
SMS_GATEWAY_IP_1 80.77.67.145

我可以发送短信,但我不明白为什么我没有收到短信。我还在代码中添加了调试语句来检查是否有任何错误。当我检查日志文件时,我收到以下信息,表明短信已发送。以前我有不同的端口号、用户名和密码,并且我能够使用相同的 java 代码发送和接收短信。但现在我需要在这个网关上发送短信,并且它也发送短信。但由于某种原因我没有收到短信。有什么办法可以检查我已发送的短信发生了什么吗?

enter image description here

下面是我的代码:

public class SMSClient
{
private static final Logger logger = LoggerFactory
.getLogger(SMSClient.class);

@Autowired
private SMSSettings smsSettings;

@Autowired
private OracleSettings oracleSettings;

/**
* If the application is bound to the SMSC.
*/
boolean _bound = false;

public boolean send(String text,
List<AlertCommunicationAddress> toAddressesSMS)
{
List<String> toAddressesSMSString = new ArrayList<String>();
for (AlertCommunicationAddress alertComAddr : toAddressesSMS)
{
List<AlertRecpGrpMember> recpMembers = alertComAddr
.getAlertRecipientsGroup().getAlertRecpGrpMembers();
for (AlertRecpGrpMember recpMem : recpMembers)
{
// check here if the member belongs to the same environment on
// which SMS is being sent.
if ((recpMem.getIsDefault() != null && recpMem.getIsDefault()
.equalsIgnoreCase("Y"))
|| (recpMem.getRunEnvironment() != null && recpMem
.getRunEnvironment().equalsIgnoreCase(
oracleSettings.getRunEnv())))
{
toAddressesSMSString.add(recpMem.getMember());
}
}

}

logger.debug("Original SMS to be sent : " + text);

String smscHost1 = smsSettings.getHost1();
Integer smscPort = smsSettings.getPort();

if (toAddressesSMSString.isEmpty())
{
return false;
}

for (String phoneNumber : toAddressesSMSString)
{

try
{
Session session = getSession(smscHost1, smscPort,
smsSettings.getUsername(), smsSettings.getPassword());
if (session == null)
{
String smscHost2 = smsSettings.getHost2();
logger.error("SMS --- Unable to get the session with Host 1 (" + smscHost1 + ":" + smscPort + ") , will try Host 2 (" + smscHost2 + ") now.");
session = getSession(smscHost2, smscPort,
smsSettings.getUsername(),
smsSettings.getPassword());

if (session == null)
{
logger.error("SMS --- Unable to get the session with Host 1 (" + smscHost1 + ") and Host 2 (" + smscHost2 + "). Please check with the SMS Gateway.");
return false;
}
}

logger.debug("SMS --- Created Session object " + session);

SubmitSM request = new SubmitSM();
request.setSourceAddr(new Address((byte) 5, (byte) 0,
"RM2Support"));
request.setDestAddr(createAddress(phoneNumber));

request.setProtocolId((byte) 0);
request.setPriorityFlag((byte) 0);
request.setRegisteredDelivery((byte) 1); // we want delivery
// reports
request.setDataCoding((byte) 0);
request.setSmDefaultMsgId((byte) 0);
// request.setScheduleDeliveryTime(deliveryTime); // you can
// skip
// this
request.setReplaceIfPresentFlag((byte) 0);

// Send the request
request.assignSequenceNumber(true);

// this is to send long messages
request.setEsmClass((byte) Data.SM_UDH_GSM);

String[] splittedMsg = splitMessage(text, 153);

int totalSegments = splittedMsg.length;

logger.debug("SMS : Number of splitted segments :: "
+ totalSegments);

// iterating on splittedMsg array. Only Sequence Number and
// short
// message text will change each time

Random random = new Random();

int randomInt = random.nextInt();

logger.debug("SMS---- Reference Number : " + randomInt);

for (int i = 0; i < totalSegments; i++)
{

ByteBuffer ed = new ByteBuffer();

ed.appendByte((byte) 5); // UDH Length

ed.appendByte((byte) 0x00); // IE Identifier

ed.appendByte((byte) 3); // IE Data Length

ed.appendByte((byte) randomInt); // Reference Number

ed.appendByte((byte) totalSegments); // Number of pieces

ed.appendByte((byte) (i + 1)); // Sequence number

ed.appendString(splittedMsg[i], Data.ENC_ASCII);

request.setShortMessageData(ed);

logger.debug("Hello...reached here...now about the submit the request::::");

SubmitSMResp response = session.submit(request);

logger.debug("SMS --- Submit response "
+ response.getCommandStatus());

// response = smsSession.submitMulti(request);
logger.debug("SMS --- Submit response "
+ response.getCommandStatus());
String messageId = response.getMessageId();
logger.debug("SMS --- Message ID = " + messageId);

}

enquireLink(session);
unbind(session);

} catch (Exception e)
{
logger.debug("Exception while sending SMS with Phone number :::" + phoneNumber + "::::" + e);
continue;
}
}

return true;
}

private Session getSession(String smscHost, int smscPort,
String smscUsername, String smscPassword) throws Exception
{
try
{

TCPIPConnection connection = new TCPIPConnection(smscHost, smscPort);
connection.setReceiveTimeout(6000);
connection.setIOBufferSize(8188);
connection.setReceiveBufferSize(8188);
Session session = new Session(connection);

// bind now

if (_bound)
{
logger.debug("Already bound, unbind first.");
return session;
}

BindRequest request = new BindTransmitter();
request.setSystemId(smscUsername);
request.setPassword(smscPassword);
// request.setSystemType(systemType);
// request.setAddressRange(addressRange);
request.setInterfaceVersion((byte) 0x34); // SMPP protocol version

logger.debug("SMS --- Bind request :: " + request.debugString());

logger.debug("SMS --- Created Session object :: " + session);

BindResponse response = session.bind(request);
logger.debug("Bind response " + response.debugString());

if (response.getCommandStatus() == Data.ESME_ROK)
{
logger.debug("SMS --- Binded with SMSC Server");
_bound = true;

} else
{
logger.error("SMS --- Unable to bind with SMSC Server :: Code :: "
+ response.getCommandStatus());
}
Integer respCode = new Integer(response.getCommandStatus());
logger.debug("SMS -- Response Code ::" + respCode);
response.setCommandStatus(respCode);
Integer comLength = new Integer(response.getCommandLength());
logger.debug("SMS -- CommandLength ::" + comLength);
response.setCommandLength(comLength);
logger.debug("SMS --- Response from SMSC" + response.toString());

return session;
} catch (WrongLengthOfStringException e)
{
logger.error("SMS -- Wrong length string exception"
+ e.getMessage());

} catch (ValueNotSetException e)
{
logger.error("SMS -- Value not set exception" + e.getMessage());

} catch (TimeoutException e)
{
logger.error("SMS -- Timeout exception " + e.getMessage());

} catch (PDUException e)
{
logger.error("SMS -- PDU exception " + e.getMessage());

} catch (WrongSessionStateException e)
{
logger.error("SMS -- Wrong Session exception " + e.getMessage());

} catch (IOException e)
{
logger.error("SMS --- Could not able to connect the host/port or Check the Username/Password for connection ::"
+ e.getMessage());
} catch (Exception e)
{
logger.error("SMS -- Error while sending SMS :: " + e.getMessage());
}
return null;
}

private Address createAddress(String address)
throws WrongLengthOfStringException
{
Address addressInst = new Address();
addressInst.setTon((byte) 5); // national ton
addressInst.setNpi((byte) 0); // numeric plan indicator
addressInst.setAddress(address, Data.SM_ADDR_LEN);
logger.debug("SMS -------- Address :: " + addressInst);
return addressInst;
}

private Session unbind(Session session)
{
try
{
if (!_bound)
{
System.out.println("Not bound, cannot unbind.");
return session;
}

// send the request
logger.debug("Going to unbind.");
if (session.getReceiver().isReceiver())
{
logger.debug("SMS --- Unbinding --- It can take a while to stop the receiver.");
}
UnbindResp response = session.unbind();
logger.debug("Unbind response " + response.debugString());
_bound = false;

} catch (Exception e)
{
logger.debug("Unbind operation failed. " + e);
}
return session;
}

/**
* Creates a new instance of <code>EnquireSM</code> class. This PDU is used
* to check that application level of the other party is alive. It can be
* sent both by SMSC and ESME.
*
* See "SMPP Protocol Specification 3.4, 4.11 ENQUIRE_LINK Operation."
*
* @see Session#enquireLink(EnquireLink)
* @see EnquireLink
* @see EnquireLinkResp
*/
private void enquireLink(Session session)
{
try
{
EnquireLink request = new EnquireLink();
EnquireLinkResp response;
logger.debug("SMS ---- Enquire Link request "
+ request.debugString());
response = session.enquireLink(request);
logger.debug("SMS --- Enquire Link response "
+ response.debugString());
} catch (Exception e)
{
logger.debug("SMS ---- Enquire Link operation failed :: " + e);
}
}

private String[] splitMessage(String s, int size)
{
if (s == null || size <= 0)
return null;
int chunks = s.length() / size + ((s.length() % size > 0) ? 1 : 0);
String[] arr = new String[chunks];
for (int i = 0, j = 0, l = s.length(); i < l; i += size, j++)
arr[j] = s.substring(i, Math.min(l, i + size));
return arr;
}
}

以下是我在发送/接收短信时需要考虑的参数。但我真的不知道JAVA OpenSMPP API是否使用此设置:

enter image description here

最佳答案

您可以使用以下代码查询 SMPP 服务器以检查您的消息(来自 https://github.com/OpenSmpp/opensmpp/blob/master/client/src/main/java/org/smpp/test/SMPPTest.java)发生了什么:

/**
* Creates a new instance of <code>QuerySM</code> class, lets you set
* subset of fields of it. This PDU is used to fetch information
* about status of already submitted message providing that you 'remember'
* message id of the submitted message. The message id is assigned
* by SMSC and is returned to you with the response to the submision
* PDU (SubmitSM, DataSM etc.).
*
* See "SMPP Protocol Specification 3.4, 4.8 QUERY_SM Operation."
* @see Session#query(QuerySM)
* @see QuerySM
* @see QuerySMResp
*/
private void query() {
debug.enter(this, "SMPPTest.query()");
try {
QuerySM request = new QuerySM();
QuerySMResp response;

// input values
messageId = getParam("Message id", messageId);
sourceAddress = getAddress("Source", sourceAddress);

// set values
request.setMessageId(messageId);
request.setSourceAddr(sourceAddress);

// send the request
System.out.println("Query request " + request.debugString());
if (asynchronous) {
session.query(request);
} else {
response = session.query(request);
System.out.println("Query response " + response.debugString());
messageId = response.getMessageId();
}

} catch (Exception e) {
event.write(e, "");
debug.write("Query operation failed. " + e);
System.out.println("Query operation failed. " + e);
} finally {
debug.exit(this);
}
}

关于未从 Java Web 应用程序接收短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41141042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com