ADURL簡化程式

來源:互聯網
上載者:User

標籤:資料流   return   訪問   dia   help   dal   net   請求   stat   

using System;using System.Diagnostics;using System.Net;using System.Text;using System.Web;using System.Linq;using System.IO;using System.Collections.Generic;namespace AdUrlConvert{class Program{public static string Finalurl;public static string getProxyIP = System.Configuration.ConfigurationManager.AppSettings["proxyIP"];public static int getproxyPORT = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["proxyPORT"]);public static string getinputCsvPath = System.Configuration.ConfigurationManager.AppSettings["inputCsvPath"];public static string getoutputCsvPath = System.Configuration.ConfigurationManager.AppSettings["outputCsvPath"];static void Main(string[] args){readUrlCsv();}public static string GetFinalAdUrl(string finlurl, string url){try{Finalurl = finlurl;string result = string.Empty;HttpWebRequest request = WebRequest.Create(finlurl) as HttpWebRequest; //第一步:用工廠機制(factory mechanism)通過Create()方法來建立一個HttpWebRequest對象。request.AllowAutoRedirect = false; // 擷取或設定一個值,該值指示請求是否應跟隨重新導向響應。WebProxy proxy = new WebProxy(getProxyIP, getproxyPORT);request.Proxy = proxy;HttpWebResponse response = request.GetResponse() as HttpWebResponse; //第二步:調用 HttpWebRequest.GetResponse()方法返回的是一個HttpWebResponse對象while (response.Headers["Location"] != null){try{Finalurl = response.Headers["Location"];response.Close();request = WebRequest.Create(Finalurl) as HttpWebRequest;request.AllowAutoRedirect = false;request.Referer = Finalurl;response = request.GetResponse() as HttpWebResponse;}catch (Exception x){Finalurl = "null";LogHelper.WriteLog(typeof(Program), string.Format("今行ってURL:{0} 詳しい內容は {1}", url, x.Message));break;}}LogHelper.WriteLog(typeof(Program), string.Format("今行ってURL:{0} 詳しい內容は {1}", url, "Finalurl:" + Finalurl));response.Close();}catch (Exception ex){LogHelper.WriteLog(typeof(Program), string.Format("今行ってURL:{0} 詳しい內容は {1}", url, ex.Message));}return Finalurl;#region 源碼的擷取//Stream sr = response.GetResponseStream(); //從Internet資源返回資料流//if (sr != null) //{// StreamReader reader = new StreamReader(sr, Encoding.UTF8); //讀取資料流// result = reader.ReadToEnd();// reader.Close();// sr.Close();// request.Abort();// response.Close();//} //url = response.Headers["Location"];//response.Close();//request = WebRequest.Create(url) as HttpWebRequest;//request.AllowAutoRedirect = false;//request.Referer = url;//response = request.GetResponse() as HttpWebResponse;//url = response.Headers["Location"];//response.Close();//request = WebRequest.Create(url) as HttpWebRequest;//request.AllowAutoRedirect = false;//request.Referer = url;//response = request.GetResponse() as HttpWebResponse;//url = response.Headers["Location"];//response.Close();//Console.WriteLine(url);#endregion 源碼的擷取}public static void readUrlCsv(){string path = getinputCsvPath;UrlList ul = new UrlList();ul.Finalurl = new List<string>();ul.Finalurl.Add("最終URL");ul.ADurlList = File.ReadAllLines(path, Encoding.UTF8).ToList();int index = 1;// 逐一查看一遍ul.ADurlList.Skip(1).ToList().ForEach(x =>{ul.Finalurl.Add(GetFinalAdUrl(ul.ADurlList[index],ul.ADurlList[index]));index++;});// 對出錯的重新訪問index = 1;ul.ADurlList.ForEach(x =>{if (ul.Finalurl[index] == null){ul.Finalurl[index] = GetFinalAdUrl(ul.ADurlList[index], ul.ADurlList[index]);index++;}});index = 0;// 產生最終的csvfor (int i = 0; i < ul.ADurlList.Count; i++){ul.ADurlList[i] += "," + ul.Finalurl[i];}File.WriteAllLines(getoutputCsvPath, ul.ADurlList);}}}

  

 

ADURL簡化程式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.