Resim kaydet isimli method parametre olarak aldığı resmi proxy kullanarak kaydeder. txtip , txtuser, txtpass değerleri ekrandan doldurulan bir textbox olarak düşünülebilir.
Çalıştığınız şirkette proxy kullanılıyorsa webrequest yapabilmeniz için sizinde kodunuzda proxy kullanmanız gerekir. Aşağıdaki gibi kullanabilirsiniz. Bir request’in stream’a atılmasınıda örneğin devamında görebilirsiniz. Stream’ide bir bitmap’e atıp kaydediyorum aşağıdaki örnekte.
public void ResmiKaydet(string picUrl)
{
try
{
Application.DoEvents();
System.Net.WebRequest request = System.Net.WebRequest.Create(picUrl);
if(txtip.Text!=""){
WebProxy proxy = new WebProxy(txtip.Text, 80);
if (txtUser.Text != "" & txtpass.Text != "")
{
proxy.Credentials = new NetworkCredential(txtUser.Text, txtpass.Text);
}
request.Proxy = proxy;
}
System.Net.WebResponse response = null;
try
{
response = request.GetResponse();
}
catch { lstError.Items.Add(picUrl);
goto devam;
}
System.IO.Stream responseStream =
response.GetResponseStream();
Bitmap bitmap2=null;
try
{
bitmap2 = new Bitmap(responseStream);
}
catch { bitmap2 = null; }
try
{
string path = Application.StartupPath+"/pics/"+picUrl.Replace('/','_').Replace(':','_');
bitmap2.Save(path);
lstClear.Items.Add(path);
}
catch(Exception ex) { lstError.Items.Add(ex.ToString()); }
devam:
bitmap2 = null;
}
catch (System.Net.WebException)
{
MessageBox.Show("Hata" ");
}
}
follow: