0

I'm trying to create an simple website to get info from another website with username and password. This is my first time programming but I couldn't find an solution to my problem. I'm trying to have an textbox on my website with a button so when you type in f. ex username and click on button, it will send WebRequest and get information from that website:

Code:

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;
    }
}

I don'tknow how to accomplish that. I have an button "Button1" but i don't know how to first type some text in textbox, click on that button and after that, send an WebRequest and get information from that website.

Thanks for all information and help!

leppie
  • 115,091
  • 17
  • 196
  • 297
DJ_TecRoot
  • 93
  • 8
  • Remove TextBox1_TextChanged and add OnClick event on button. Move the code from Page_Load to btn_OnClick. Replace html != string.Empty && html != null with !String.IsNullOrEmpty(html) – Emanuele Apr 19 '16 at 13:22
  • Thanks! Worked! Another question - I have som string I would like to display in GridView or some sort of table: 90225 David Information like 90225 is usernumber and David is an Username. How can I convert those strings in to an Gridview or table? – DJ_TecRoot Apr 19 '16 at 13:49

1 Answers1

0

You are trying to authenticate on another page with login and password inserted on your page right?

Depending on how authentication is implemented you could use something like this.

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();
}
Florian Lim
  • 5,332
  • 2
  • 27
  • 28
Cezary
  • 121
  • 4