0

我正在尝试创建一个简单的网站,以使用用户名和密码从另一个网站获取信息。这是我第一次编程,但我找不到解决问题的方法。我试图在我的网站上有一个带有按钮的文本框,所以当你输入 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 并从该网站获取信息。

感谢所有信息和帮助!

4

1 回答 1

0

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

根据身份验证的实现方式,您可以使用类似的东西。

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();
}
于 2016-04-19T13:26:21.627 回答