【问题标题】:How to use String in DirectCast in Visual Basic 2013?如何在 Visual Basic 2013 的 DirectCast 中使用字符串?
【发布时间】:2014-07-21 14:11:37
【问题描述】:

我正在尝试使用以下代码为 Json 进行 DirectCast -

Dim EmailId as String
Dim URL as String
EmailId = txtEmailId.Text
URL = "http://localhost/json.php?id=" & EmailId

request = DirectCast(URL, HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())

问题是,上述方法只有在我将电子邮件 ID 硬编码到 URL 时才有效。例如DirectCast("http://localhost/json.php?id=abcd@gmail.com", HttpWebRequest)。当尝试将电子邮件 ID 作为变量时,我收到以下错误 -

“字符串”类型的值无法转换为 'System.Net.HttpWebRequest'。

请帮助我完成这项工作。

【问题讨论】:

  • DirectCast 不是从某事物到其他事物的转换工具。
  • 如果DirectCast(URL, HttpWebRequest) 不工作,我高度怀疑DirectCast("http://localhost/json.php?id=abcd@gmail.com", HttpWebRequest) 工作。您可能需要仔细检查。
  • @roryap 感谢您提醒我仔细检查。我忘了在DirectCast(WebRequest.Create(URL), HttpWebRequest) 中添加WebRequest.Create(URL)。谢谢。

标签: vb.net visual-studio-2010 visual-studio-2013 json.net directcast


【解决方案1】:
request = WebRequest.Create(URL)

【讨论】:

  • 我正要发布相同的内容,但我忘记在DirectCast(WebRequest.Create(URL), HttpWebRequest) 中添加WebRequest.Create(URL)。谢谢。
  • 我一定会的。我正在等待 5 分钟的 stackoverflow 时间限制完成:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
相关资源
最近更新 更多