C# http get请求 判断是否存在该图片信息
程序员文章站
2022-03-21 14:21:06
...
/// <summary> /// http get请求 /// </summary> /// <param name="Url"></param> /// <param name="postDataStr"></param> /// <returns></returns> public string HttpGet(string Url, string postDataStr) { string retString = string.Empty; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + postDataStr); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); } catch (Exception ex) { if (!File.Exists(Server.MapPath("~/LogFile/" + DateTime.Now.ToString("yyyy-MM") + ".txt"))) { FileStream fs = new FileStream(Server.MapPath("~/LogFile/" + DateTime.Now.ToString("yyyy-MM") + ".txt"), FileMode.Create, FileAccess.Write); //可以指定盘符,也可以指定任意文件名,还可以为word等文件 StreamWriter sw = new StreamWriter(fs); // 创建写入流 sw.WriteLine(ex.Message); // 写入Hello World sw.Close(); //关闭文件 } else { FileStream fs = new FileStream(Server.MapPath("~/LogFile/" + DateTime.Now.ToString("yyyy-MM") + ".txt"), FileMode.Open, FileAccess.Write); StreamWriter sr = new StreamWriter(fs); sr.WriteLine(ex.Message);//开始写入值 sr.Close(); fs.Close(); } } return retString; }
下一篇: CSS css