ADURL简化程序

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;
// 生成最终的csv

for (int i = 0; i < ul.ADurlList.Count; i++)
{
ul.ADurlList[i] += "," + ul.Finalurl[i];
}
File.WriteAllLines(getoutputCsvPath, ul.ADurlList);
}
}
}

  

 

posted @ 2016-12-15 11:50  公众号python学习开发  阅读(460)  评论(0编辑  收藏  举报