1
votes

i converted a vb.net code to c# to use a given api to enable the sms functionality, ive been having this error on this part of my code 'myWebRequest = System.Net.WebRequest.Create(sURL);'

ERROR

here is my code

protected void Button1_Click(object sender, EventArgs e)
    {
        HttpWebRequest myWebRequest = null;
        HttpWebResponse myWebResponse = null;
        try
        {
            string sURL = "sample/api.aspx";
            sURL = sURL + "?apiusername=" + HttpUtility.UrlEncode("123");
            sURL = sURL + "&apipassword=" + HttpUtility.UrlEncode("xyz");
            sURL = sURL + "&mobileno=" + HttpUtility.UrlEncode("6141234567");
            sURL = sURL + "&senderid=" + HttpUtility.UrlEncode("try");
            sURL = sURL + "&languagetype=" + "1";
            sURL = sURL + "&message=" + HttpUtility.UrlEncode("testing sms from api");
            myWebRequest = HttpWebRequest.Create(sURL);
            myWebResponse = myWebRequest.GetResponse();
            if (myWebResponse.StatusCode == HttpStatusCode.OK)
            {
                Stream oStream = myWebResponse.GetResponseStream;
                StreamReader oReader = new StreamReader(oStream);
                string sResult = oReader.ReadToEnd();
                if (long.Parse(sResult) > 0)
                {
                    Response.Write("success - MT ID :" + sResult);
                }
                else
                {
                    Response.Write("fail - Error code :" + sResult);
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("Some issue happen");
        }
        finally
        {
            if ((myWebResponse != null))
            {
                myWebResponse.Close();
            }
        }
    }

here is the main code from VB.net

Imports System.Net

Imports System.IO

Partial Class _Default Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myWebRequest As HttpWebRequest = Nothing
    Dim myWebResponse As HttpWebResponse = Nothing
    Try
        Dim sURL As String = "sample/api.aspx"
        sURL = sURL & "?apiusername=" & HttpUtility.UrlEncode("123")
        sURL = sURL & "&apipassword=" & HttpUtility.UrlEncode("xyz")
        sURL = sURL & "&mobileno=" & HttpUtility.UrlEncode("6141234567")
        sURL = sURL & "&senderid=" & HttpUtility.UrlEncode("try")
        sURL = sURL & "&languagetype=" & "1"
        sURL = sURL & "&message=" & HttpUtility.UrlEncode("testing sms from api")
        myWebRequest = System.Net.WebRequest.Create(sURL)
        myWebResponse = myWebRequest.GetResponse()
        If myWebResponse.StatusCode = HttpStatusCode.OK Then
            Dim oStream As Stream = myWebResponse.GetResponseStream
            Dim oReader As StreamReader = New StreamReader(oStream)
            Dim sResult As String = oReader.ReadToEnd
            If Long.Parse(sResult) > 0 Then
                Response.Write("success - MT ID :" & sResult)
            Else
                Response.Write("fail - Error code :" & sResult)
            End If
        End If
    Catch ex As Exception
        Response.Write("Some issue happen")
    Finally
        If Not myWebResponse Is Nothing Then
            myWebResponse.Close()
        End If
    End Try
End Sub

End Class

2
I tried your code, and it seems that HttpWebRequest.Create() method returns a WebRequest type, which causes the error. Change your HttpWebRequest myWebRequest to WebRequest myWebRequestCyval
can you post your vb code too ?hud
ok will update my postevil.toaster

2 Answers

0
votes

The case is, HttpWebRequest returns a WebRequest type, but your myWebRequest is a type of HttpWebRequest.

Notice your VB vs. C# lines. Both myWebRequest objects are of HttpWebRequest type. However:

myWebRequest = HttpWebRequest.Create(sURL); <-- This is C#
myWebRequest = System.Net.WebRequest.Create(sURL) <-- This is VB.NET

From there, you may change your C# code to

myWebRequest = System.Net.WebRequest.Create(sURL)
0
votes
protected void Button1_Click(object sender, EventArgs e)
{
      HttpWebRequest myWebRequest = null;
      HttpWebResponse myWebResponse = null;
      try
      {
          string sURL = "sample/api.aspx";
          sURL = sURL + "?apiusername=" + HttpUtility.UrlEncode("123");
          sURL = sURL + "&apipassword=" + HttpUtility.UrlEncode("xyz");
          sURL = sURL + "&mobileno=" + HttpUtility.UrlEncode("6141234567");
          sURL = sURL + "&senderid=" + HttpUtility.UrlEncode("try");
          sURL = sURL + "&languagetype=" + "1";
          sURL = sURL + "&message=" + HttpUtility.UrlEncode("testing sms from api");
          myWebRequest = (HttpWebRequest)System.Net.WebRequest.Create(sURL);
          myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
          if (myWebResponse.StatusCode == HttpStatusCode.OK)
          {
              Stream oStream = myWebResponse.GetResponseStream();
              StreamReader oReader = new StreamReader(oStream);
              string sResult = oReader.ReadToEnd();
              if (long.Parse(sResult) > 0)
              {
                  Response.Write("success - MT ID :" + sResult);
              }
              else
              {
                  Response.Write("fail - Error code :" + sResult);
              }
          }
      }
      catch (Exception ex)
      {
            Response.Write("Some issue happen");
      }
      finally
      {
          if ((myWebResponse != null))
          {
                myWebResponse.Close();
          }
      }
 }