ASP.net System.Net.Webrequest to System.Net.Httpwebrequest e
本文关键字:System Net Httpwebrequest to Webrequest ASP net | 更新日期: 2023-09-27 18:28:56
我将vb.net
代码转换为c#
,以使用给定的api来启用短信功能,我在代码'myWebRequest = System.Net.WebRequest.Create(sURL);'
的这一部分遇到了这个错误
错误
这是我的代码
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();
}
}
}
这是VB.net 的主要代码
Imports System.Net
导入System.IO
部分类别故障(_D)继承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
最终类
情况是,HttpWebRequest
返回WebRequest
类型,但您的myWebRequest
是HttpWebRequest.
类型
注意你的VB和C#行。两个myWebRequest
对象都属于HttpWebRequest
类型。但是:
myWebRequest = HttpWebRequest.Create(sURL); <-- This is C#
myWebRequest = System.Net.WebRequest.Create(sURL) <-- This is VB.NET
从那里,您可以将C#代码更改为
myWebRequest = System.Net.WebRequest.Create(sURL)
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();
}
}
}