网络图片、CSV文件请求读取转DataTable
1、读取网络图片,获取图片宽高
string path = “http://xxx.jpg” //图片路径;
Uri mUri = new Uri(path);http://xxx.jpg
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(mUri);
mRequest.Method = "GET";
mRequest.Timeout = 200;
mRequest.ContentType = "text/html;charset=utf-8";
HttpWebResponse mResponse = (HttpWebResponse)mRequest.GetResponse();
Stream mStream = mResponse.GetResponseStream();
string aSize = (mResponse.ContentLength / 1024).ToString() + "KB";
Image mImage = Image.FromStream(mStream);
string aLength = mImage.Width.ToString() + "x" + mImage.Height.ToString();
2、读取网络CSV文件,数据转为DataTable处理
Uri mUri = new Uri(“http://xxx.csv");
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(mUri);
mRequest.Method = "GET";
mRequest.Timeout = 20000;
mRequest.ContentType = "text/html;charset=utf-8";
HttpWebResponse mResponse = (HttpWebResponse)mRequest.GetResponse();
Stream mStream = mResponse.GetResponseStream();
StreamReader sr = new StreamReader(mStream);
string strTitle = "x,y,value";
string[] strColumTitle = strTitle.Split(','); //CVS 文件默认以逗号隔开
for (int i = 0; i < strColumTitle.Length; i++)
{
dt.Columns.Add(strColumTitle[i]);
}
while (!sr.EndOfStream)
{
string strTest = sr.ReadLine().Trim('\0');
if (!string.IsNullOrWhiteSpace(strTest))
{
DataRow dr = dt.NewRow();
if (dt.Columns.Count == 1)
{
dr[0] = strTest;
}
else
{
string[] strTestAttribute = strTest.Split(',');
for (int i = 0; i < strColumTitle.Length; i++)
{
dr[strColumTitle[i]] = strTestAttribute[i];
}
}
dt.Rows.Add(dr);
}
sr.Close();