2016-08-30 19 views
0

私はアプリケーションにSMSが必要なvb.netアプリケーションに取り組んでいます。 SMSサービスプロバイダAPI XML APIを使用して多数のメッセージを送信するように求められました。また、XML APIを使用して、各番号ごとに異なるメッセージをカスタマイズできます。VB.NETのサーバーにXMLを投稿

Sample XML format as per SMS Provider: 
<MESSAGE> 
    <AUTHKEY>Authentication Key </AUTHKEY> 
    <SENDER>SenderID</SENDER> 
    <ROUTE>Template</ROUTE> 
    <CAMPAIGN>XML API</CAMPAIGN> 
    <COUNTRY>country code</COUNTRY> 
    <SMS TEXT="message1" > 
     <ADDRESS TO="number1"></ADDRESS> 
    </SMS> 
    <SMS TEXT="hi test message" > 
     <ADDRESS TO="number2"></ADDRESS> 
    </SMS> 
</MESSAGE> 
Post your request with above format in data variable. 
http://api.msg91.com/api/postsms.php 

プロバイダdntにはVB.NET用のサンプルコードがありますので、多くの検索の結果、最終的にVBでHttpWebRequestを使用する際の情報が得られました。 &は、コードを入れますが、その出力「コード:201」第一ランエラーメッセージで

Imports System.Data 
    Imports System.Data.OleDb 
    Imports System.Globalization 
    Imports System.Text 
    Imports System.IO 
    Imports System.Net 
    Imports System.Web 
    Imports System.Xml 

    Dim authKey As String 
    Dim mobile As String 
    Dim senderId As String 
    Dim route As String 
    Dim URLXML As String = "http://api.msg91.com/api/postsms.php?data=" 

     'Set these variables 
     authKey = "XXXXXXXXXXX" 
     mobile = String.Empty 
     'Sender ID, While using route4 sender id should be 6 characters long. 
     senderId = "XXXXXX" 
     'Define route 
     route = "X" 

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click 
     If (DataGridView2.Rows.Count > 0) Then 
      Dim xml As System.Text.StringBuilder = New System.Text.StringBuilder() 
      xml.Append("<MESSAGE>" & Environment.NewLine) 
      xml.Append("<AUTHKEY>" & authKey & "</AUTHKEY>" & Environment.NewLine) 
      xml.Append("<SENDER>" & senderId.ToString & "</SENDER>" & Environment.NewLine) 
      xml.Append("<ROUTE>" & route.ToString & "</ROUTE>" & Environment.NewLine) 
      xml.Append("<COUNTRY>91</COUNTRY>" & Environment.NewLine) 

      'MOBILE & MESSAGE FIELDS LOADED FROM DATAGRIDVIEW ROWS 
      For i As Integer = 0 To DataGridView2.Rows.Count - 1 
        xml.Append("<SMS TEXT='" & URLEncode(DataGridView2.Rows(i).Cells("MESSAGE").Value.ToString) & "'>" & Environment.NewLine) 
        xml.Append("<ADDRESS TO='" & DataGridView2.Rows(i).Cells("MOBILE").Value.ToString & "'></ADDRESS>") 
        xml.Append("</SMS>" & Environment.NewLine) 
      Next 
      xml.Append("</MESSAGE>") 

      'URLEncode Whole input String as told by the SMS Provider 
      Dim xmlData As String = URLEncode(xml.ToString) 
      ' Create POST data and convert it to a byte array. 
      Dim encoding As New UTF8Encoding 
      Dim bytes As Byte() = encoding.GetBytes(xmlData) 

      Try 
       Dim req As HttpWebRequest = DirectCast(WebRequest.Create(URLXML), HttpWebRequest) 
       req.Method = "POST" 
       ' Set the ContentType property of the WebRequest. 
       req.ContentType = "application/x-www-form-urlencoded" 
       ' Set the ContentLength property of the WebRequest. 
       req.ContentLength = bytes.Length 
       ' Get the request stream. 
       Using dataStream As Stream = req.GetRequestStream() 
        dataStream.Write(bytes, 0, bytes.Length) 
       End Using 


       ' Get the response. 
       'Dim response As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse) 
       Dim response As HttpWebResponse = req.GetResponse() 
       If (response.StatusCode = HttpStatusCode.OK) Then 
        ' Display the status. 
        ' Get the stream containing content returned by the server. 
        Dim dStream As Stream = response.GetResponseStream() 
        ' Open the stream using a StreamReader for easy access. 
        Dim reader As New StreamReader(dStream, True) 
        ' Read the content 
        Dim responseFromServer As String = reader.ReadToEnd() 
        ' Display the content. 
        MsgBox(responseFromServer.ToString) 
        reader.Close() 
        dStream.Close() 
       End If 
       ' Clean up & close 
       response.Close() 
      Catch ex As Exception 
       MsgBox(ex.ToString) 
      End Try 
     End If 
    End Sub 

Public Function URLEncode(ByVal Text As String) As String 
     Dim i As Integer 
     Dim acode As Integer 
     'Dim chars As String 
     URLEncode = Text 
     For i = Len(URLEncode) To 1 Step -1 
      acode = Asc(Mid$(URLEncode, i, 1)) 
      Select Case acode 
       Case 10 
        'replace line break to "0A" 
        Mid$(URLEncode, i, 1) = "0A" 
       Case 47 To 57, 65 To 90, 97 To 122 
        ' don't touch alphanumeric chars 
       Case 32 
        ' replace space with "+" 
        Mid$(URLEncode, i, 1) = "+" 
       Case Else 
        ' replace punctuation chars with "%hex" 
        URLEncode = Left$(URLEncode, i - 1) & "%" & Hex$(acode) & Mid$ _ 
         (URLEncode, i + 1) 
      End Select 
     Next 
End Function 

が表示されます:201 1エラーメッセージも即時に表示されます。

System.Net.WebException: The remote name could not be resolved": 'api.msg91.com' at System.Net.http.WebRequest.GetRequestStream 

&二度目の出力がコードでありますウィンドウ "System.Net.WebException '型の最初のチャンス例外はSystem.dllで発生しました"。

SMSプロバイダがVB.NET用のコードサンプルを持っているので、私にこのlinkを送ってきました。

1)
2)変更コンテンツタイプ全XMLストリングにNO URLENCODE:text/plainの
3)が追加され
4)の代わりにストリームのStreamWriterを使用しreq.timeoutそれからコードあたりのようないくつかの変更を加え。

Dim req As HttpWebRequest = WebRequest.Create(URLXML) 
     req.Method = WebRequestMethods.Http.Post 
     ' Set the ContentType property of the WebRequest. 
     'req.ContentType = "application/x-www-form-urlencoded" 
     req.ContentType = "text/plain" 
     ' Set the ContentLength property of the WebRequest. 
     req.ContentLength = xml.Length 
     req.Timeout = 1000000 
     ' Get the request stream. 
     Dim sw As New StreamWriter(req.GetRequestStream) 
     sw.Write(xml.ToString) 
     sw.Close() 

このコードでは、WebExceptionまたはimmmediateウィンドウエラーは発生しませんが、同じ出力コード:201があります。 ドキュメントごと。出力は5134842646923e183d000075である必要があります。 出力は、英数字の上記のような24文字の要求IDです。このリクエストIDを使用すると、配信レポートを表示できます。要求が正常に送信されなかった場合、適切なエラーメッセージが表示されます。

すべてのユーザーに同じテキストメッセージを送信するプロバイダのother methodは動作していますが、送信する必要がある場合はループを使用してコードを実行するのに要求数が多い場合は、XML APIを使用する必要があります。 XMLメソッドがデータをポストしていない理由を理解できません。コード内のエラーや間違いは何ですか?ありがとう。

EDIT: はまた、コンテンツタイプのプロパティに& ASCIIEncodingが、同じ出力を変更しよう:

req.ContentType = "application/x-www-form-urlencoded" 
+0

https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success 201 =「要求が新しいリソースの創造をもたらす、満たされた」 – Jeremy

+0

はい、データは掲載されていません。ドキュメントごとに。出力は次のようにする必要があります。5134842646923e183d000075 注:出力IDは、英数字で、上記のように24文字が含まれます。このリクエストIDを使用すると、配信レポートを表示できます。リクエストが正常に送信されなかった場合、適切なエラーメッセージが表示されます。 – Vehlad

答えて

0

私だけで使用msg91サービスと同じ問題にしようとの長い一日の後、私は次のようで成功しを得ましたコード

Imports System 
Imports System.IO 
Imports System.Net 
Imports System.Text 
Public Class Form1 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

     ' Create a request using a URL that can receive a post. 
     Dim request As WebRequest = WebRequest.Create("http://sms.orreryhim.com/api/postsms.php") 
     ' Set the Method property of the request to POST. 
     request.Method = "POST" 
     ' Create POST data and convert it to a byte array. 
     Dim postData As String = "<MESSAGE> 
    <AUTHKEY>your authkey here</AUTHKEY> 
    <ROUTE>route code</ROUTE> 
    <CAMPAIGN>campaign mode</CAMPAIGN> 
    <COUNTRY>country code</COUNTRY> 
    <SENDER>sender id</SENDER> 
    <SMS TEXT='"messges'"> 
     <ADDRESS TO="'mobile number'"></ADDRESS> 
    </SMS> 
</MESSAGE>" 
     Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) 
     ' Set the ContentType property of the WebRequest. 
     request.ContentType = "application/xml" 
     ' Set the ContentLength property of the WebRequest. 
     request.ContentLength = byteArray.Length 
     ' Get the request stream. 
     Dim dataStream As Stream = request.GetRequestStream() 
     ' Write the data to the request stream. 
     dataStream.Write(byteArray, 0, byteArray.Length) 
     ' Close the Stream object. 
     dataStream.Close() 
     ' Get the response. 
     Dim response As WebResponse = request.GetResponse() 
     ' Display the status. 
     'Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) 
     Label1.Text = CType(response, HttpWebResponse).StatusDescription 
     ' Get the stream containing content returned by the server. 
     dataStream = response.GetResponseStream() 
     ' Open the stream using a StreamReader for easy access. 
     Dim reader As New StreamReader(dataStream) 
     ' Read the content. 
     Dim responseFromServer As String = reader.ReadToEnd() 
     ' Display the content. 
     Console.WriteLine(responseFromServer) 
     Label2.Text = responseFromServer 
     ' Clean up the streams. 
     reader.Close() 
     dataStream.Close() 
     response.Close() 


    End Sub 

End Class 
+0

Welcome to Stackoverflow! [良い答え](https://stackoverflow.com/help/how-to-answer)を提供するために、あなたの調査結果を詳細に検討してください。このコードは質問に答えるかもしれませんが、なぜこのコードが質問に答えるか、および/またはどのようにして追加の文脈を提供すれば、長期的な価値が向上します。 –

関連する問題