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);'
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
HttpWebRequest.Create()
method returns aWebRequest
type, which causes the error. Change yourHttpWebRequest myWebRequest
toWebRequest myWebRequest
– Cyvalvb
code too ? – hud