【问题标题】:Using text in Textbox before sending WebRequest在发送 WebRequest 之前在文本框中使用文本
【发布时间】:2016-08-11 16:51:52
【问题描述】:

我正在尝试创建一个简单的网站,以使用用户名和密码从另一个网站获取信息。这是我第一次编程,但我找不到解决问题的方法。我试图在我的网站上有一个带有按钮的文本框,所以当你输入 f 时。 ex 用户名并单击按钮,它将发送 WebRequest 并从该网站获取信息:

代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void TextBox1_TextChanged(object sender, EventArgs e)
   {

   }
protected void Page_Load(object sender, EventArgs e)
      {


    WebRequest request = (WebRequest)WebRequest.Create("https://hub.tec.net/Custom/SP/MSKR/hubtest1?userid=" + TextBox1.Text);
    request.Credentials = new System.Net.NetworkCredential("myuser", "tPeVo4O5Ph13VbdVoFNIlTZvKND2Ax65");
    WebResponse response = request.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream());
    string html = sr.ReadToEnd();
    sr.Close();
    response.Close();

    if (html != string.Empty && html != null)
    {
        Literal1.Text = html;
    }
}

我不知道如何做到这一点。我有一个按钮“Button1”,但我不知道如何先在文本框中输入一些文本,然后单击该按钮,然后发送 WebRequest 并从该网站获取信息。

感谢所有信息和帮助!

【问题讨论】:

  • 移除 TextBox1_TextChanged 并在按钮上添加 OnClick 事件。将代码从 Page_Load 移动到 btn_OnClick。将 html != string.Empty && html != null 替换为 !String.IsNullOrEmpty(html)
  • 谢谢!工作!另一个问题 - 我有一些字符串,我想在 GridView 或某种表格中显示: 90225David 类似 90225 的信息是用户编号,大卫是用户名。如何将这些字符串转换为 Gridview 或表格?

标签: c# asp.net get-request


【解决方案1】:

您正在尝试在另一个页面上进行身份验证,并在您的页面上插入了登录名和密码,对吗?

根据身份验证的实施方式,您可以使用类似的方法。

public void OnPostInfoClick(object sender, System.EventArgs e)
{

    string strId = "demo";
    string strName = "password";
    string firstname = "John";
    string lastname = "Smith";

    ASCIIEncoding encoding = new ASCIIEncoding();
    string postData = "userid=" + strId;
    postData += ("&username=" + strName);
    postData += ("&firstname=" + firstname);
    postData += ("&lastname=" + lastname);
    byte[] data = encoding.GetBytes(postData);

    HttpWebRequest myRequest =
      (HttpWebRequest)WebRequest.Create("http://www.mywebpage.com/casting/include/callremote.asp");
    myRequest.Method = "POST";
    myRequest.ContentType = "application/x-www-form-urlencoded";
    myRequest.ContentLength = data.Length;
    Stream newStream = myRequest.GetRequestStream();

    newStream.Write(data, 0, data.Length);
    newStream.Close();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多