Esempio n. 1
0
        private void DownloadImage(string objURL)
        {
            string destFile = Path.Combine(destDir, Path.GetFileName(objURL));

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(objURL);

            request.Referer = "http://image.baidu.com";

            if (txtLog.IsHandleCreated)
            {
                txtLog.BeginInvoke(new Action(() =>
                {
                    txtLog.AppendText("正在下载" + objURL + Environment.NewLine);
                }));
            }
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        using (Stream stream = response.GetResponseStream())
                            using (Stream fsStream = new FileStream(destFile, FileMode.Create))
                            {
                                stream.CopyTo(fsStream);
                                if (txtLog.IsHandleCreated)
                                {
                                    txtLog.BeginInvoke(new Action(() =>
                                    {
                                        txtLog.AppendText("已保存到:" + destFile + Environment.NewLine + Environment.NewLine);
                                    }));
                                }
                            }
                    }
                    else
                    {
                        if (txtLog.IsHandleCreated)
                        {
                            txtLog.BeginInvoke(new Action(() =>
                            {
                                txtLog.AppendText("下载" + objURL + "失败,错误码:" + response.GetHashCode() + Environment.NewLine);
                            }));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (txtLog.IsHandleCreated)
                {
                    txtLog.BeginInvoke(new Action(() =>
                    {
                        txtLog.AppendText(ex.Message + Environment.NewLine);
                    }));
                }
                //txtLog.AppendText("下载" + objURL + "失败!" + Environment.NewLine);
            }
        }
Esempio n. 2
0
        public virtual void LogResponse(HttpWebResponse response)
        {
            int responseId = response.GetHashCode();
            string data = Util.ReadTextFromHttpResponse(response);
            string statusCode = response.StatusCode.ToString();
            string headerString = BuildHeadersString(response.Headers);
            if (testFixture.CurrentNode != null && (testFixture.CurrentNode is Request) 
                && ((Request)testFixture.CurrentNode).Response == null)
            {
                Response responseInfo = new Response();
                responseInfo.HttpCode = statusCode;
                responseInfo.Data = data;
                responseInfo.Headers = headerString;
                responseInfo.Id = responseId.ToString();

                (testFixture.CurrentNode as Request).Response = responseInfo;

            }
        }
Esempio n. 3
0
 public override int GetHashCode()
 {
     return(innerResponse.GetHashCode());
 }