我正在尝试创建一个简单的网站,以便从其他网站获取用户名和密码的信息。这是我第一次编程,但我找不到解决问题的方法。我正试图在我的网站上有一个带有按钮的文本框,所以当你输入f。如果用户名并点击按钮,它将发送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并从该网站获取信息。
感谢所有信息和帮助!
答案 0 :(得分: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();
}
答案 1 :(得分:0)
谢谢!成功了!另一个问题 - 我想要在GridView或某种表格中显示som字符串:
<ArrayOfstring> <string>90225</string> <string>David</string> </ArrayOfstring> </ArrayOfArrayOfstring>
90225之类的信息是用户名,David是用户名。如何在代码中将这些字符串转换为Gridview或表?