如何将'System.Net.FileWebRequest'类型的对象转换为'System.Net.HttpWebRequest'类型

时间:2015-08-01 10:02:24

标签: c# httpwebrequest

我收到上面提到的错误,因为我刚刚将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();

1 个答案:

答案 0 :(得分:0)

WebRequest.Create()的文档中所述,此方法返回WebRequest类的后代,具体取决于方法参数中指定的Uri模式。对于“http://”模式,它返回HttpWebRequest的实例,因此您必须将值转换为appripriate类型。要执行cast in 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();
        .......
    }