如何强制转换类型为'System.Net.FileWebRequest'键入'System.Ne

本文关键字:System Net FileWebRequest 键入 Ne 何强制 转换 类型 | 更新日期: 2023-09-27 18:05:55

我得到上面我提到的错误,因为我刚刚转换vb代码到csharp。请问是否有人知道如何解决这个问题?提前感谢

 HttpWebRequest myWebRequest = null;
    HttpWebResponse myWebResponse = null;
       string strNo = null;
    strNo = TextBox1.Text;
    try {
        string sURL = "http://gateway.onewaysms.sg:10002/api.aspx";
        sURL = sURL + "?apiusername=" + HttpUtility.UrlEncode("API3YS3NVIWNP");
        sURL = sURL + "&apipassword=" + HttpUtility.UrlEncode("API3YS3NVIWNPGNZIV");
        sURL = sURL + "&mobileno=" + HttpUtility.UrlEncode(strNo);
        sURL = sURL + "&senderid=" + HttpUtility.UrlEncode("onewaysms");
        sURL = sURL + "&languagetype=" + "1";
        sURL = sURL + "&message=" + HttpUtility.UrlEncode("testing");

      //Error at WebRequest   
        myWebRequest = System.Net.WebRequest.Create(sURL);
        myWebResponse = myWebRequest.GetResponse();

如何强制转换类型为'System.Net.FileWebRequest'键入'System.Ne

正如WebRequest. create()的文档中所描述的那样,该方法返回WebRequest类的后代,这取决于方法参数中指定的Uri模式。对于"http://"模式,它返回HttpWebRequest的实例,因此您必须将值转换为适当的类型。要在c#中执行强制转换,请在要转换的值前面的括号中指定要强制转换的类型,如下所示。同样,你必须将myWebRequest.GetResponse()的结果强制转换为HttpWebResponse.

    string strNo = TextBox1.Text;
    try
    {
        string sURL = "http://gateway.onewaysms.sg:10002/api.aspx";
        sURL = sURL + "?apiusername=" + HttpUtility.UrlEncode("API3YS3NVIWNP");
        sURL = sURL + "&apipassword=" + HttpUtility.UrlEncode("API3YS3NVIWNPGNZIV");
        sURL = sURL + "&mobileno=" + HttpUtility.UrlEncode(strNo);
        sURL = sURL + "&senderid=" + HttpUtility.UrlEncode("onewaysms");
        sURL = sURL + "&languagetype=" + "1";
        sURL = sURL + "&message=" + HttpUtility.UrlEncode("testing");
        HttpWebRequest myWebRequest = (HttpWebRequest)System.Net.WebRequest.Create(sURL);
        HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
        .......
    }