ASP.net System.Net.Webrequest到System.Net.Httpwebrequest错误

时间:2016-01-05 05:57:27

标签: c# asp.net vb.net api

我将vb.net代码转换为c#以使用给定的api启用短信功能,我的代码'myWebRequest = System.Net.WebRequest.Create(sURL);'的这部分已经出现此错误

ERROR

这是我的代码

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

Imports System.IO

Partial Class _Default     继承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

结束班

2 个答案:

答案 0 :(得分:0)

案例是,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)

答案 1 :(得分:0)

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();
          }
      }
 }