當前位置:編程學習大全網 - 源碼下載 - C# 中怎麽把已知網址頁面轉化為字符串

C# 中怎麽把已知網址頁面轉化為字符串

如果直接用代碼,不用第三方控件(如ie核心),那需要用到/Download/index.aspx?lang=cn"; //全部網址

string url = ""; //域名,路徑,文件組成的url

string queryString = ""; //查詢字符串參數

string[] valiateHttProtocol;

if (txbRequest.Text.Trim()!="")

{

requestParam = txbRequest.Text.Trim();

}

else

txbRequest.Text = requestParam;

valiateHttProtocol = requestParam.Split('/');

if (requestParam.IndexOf("?") > 0)

{

url = requestParam.Substring(0, requestParam.IndexOf("?"));

queryString = requestParam.Substring(requestParam.IndexOf("?") + 1);

}

else

{

if (valiateHttProtocol[0] == "patible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)";

myRequest.Method = "post";

myRequest.ContentType = "application/x-www-form-urlencoded";

myRequest.ContentLength = byteQueryString.Length;

Stream streamWriteRequest = myRequest.GetRequestStream();

streamWriteRequest.Write(byteQueryString, 0, byteQueryString.Length);

streamWriteRequest.Close();

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

StreamReader sr = new StreamReader(myResponse.GetResponseStream());

string sResponse = sr.ReadToEnd();

rtxbShowResponse.Text = sResponse;

btnSendRequest.Enabled = true;

}

catch(Exception e1)

{

rtxbShowResponse.Text = "無法得到到請求頁源文件,可能是以下原因:" + "\r"

+ "1.網址格式不正確;" + "\r"

+ "2.無法發出請求或請求被拒絕;" + "\r"

+ "3.請求的網站沒有響應。" + "\r"

+ e1.ToString();

btnSendRequest.Enabled = true;

}

/*

妳需要在form上面

1,拖壹個richTextBox,改名為rtxbShowResponse,用來顯示網頁源代碼

2,拖壹個TextBox,改名為txbRequest,用來輸入網頁地址

3,拖壹個Button,改名為btnSendRequest,發送url請求

*/

  • 上一篇:如何看待微信小程序?
  • 下一篇:ext2exploder怎麽修改linux的系統文件
  • copyright 2024編程學習大全網