using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.io;
using system.linq;
using system.net;
using system.text;
using system.threading;
using system.threading.tasks;
using system.windows.forms;
using newtonsoft.json;
using newtonsoft.json.linq;
namespace 图片下载器 {
public partial class form1 : form {
private string dir;
public form1() {
control.checkforillegalcrossthreadcalls = false;//这种方法不推荐使用,即不检查跨线程操作,应该使用委托的
initializecomponent();
}
private void butselect_click(object sender , eventargs e) {
folderbrowserdialog dlg = new folderbrowserdialog();
if (dlg.showdialog() == system.windows.forms.dialogresult.ok) {
textdir.text = dlg.selectedpath;
}
}
public static int pagecount = 1;
private void showpages() {
this.textshow.appendtext(目前正在下载第 + pagecount + 页\n);
}
private void butstart_click(object sender , eventargs e) {
string key = textkeywords.text;
if (string.isnullorempty(key)) {//检测关键字
messagebox.show(请输入关键词!);
return;
}
if (string.isnullorempty(textdir.text)) {//检测路径
messagebox.show(请选择路径!);
return;
}
dir = textdir.text;
if (!dir.endswith(\\)) {
dir = dir + \\;
}
thread thread = new thread(() => {//启动一个新线程
process(key);
});
thread.start();//线程启动
}
private void process(string key) {
int count = (int)numericupdown.value;//请求的页面数量
for (int i = 0 ; i pagecount = i + 1;
showpages();
httpwebrequest req = (httpwebrequest)webrequest.create(http://image.baidu.com/search/avatarjson?tn=resultjsonavatarnew&ie=utf-8&word= + uri.escapeuristring(key) + &cg=girl&pn= + (i + 1) * 60 + &rn=60&itg=0&z=0&fr=&width=&height=&lm=-1&ic=0&s=0&st=-1&gsm=360600003c);
using (httpwebresponse res = (httpwebresponse)req.getresponse()) {
if (res.statuscode == httpstatuscode.ok) {
using (stream stream = res.getresponsestream()) {
try {
download(stream);
} catch (exception e) {
textshow.begininvoke(new action(() => {
textshow.appendtext(e.message + environment.newline);
}));
}
}
} else {
messagebox.show(获取第 + i + 页失败! + res.statuscode);
}
}
}
}
private void download(stream stream) {
using (streamreader reader = new streamreader(stream)) {
string json = reader.readtoend();
jobject objroot = (jobject)jsonconvert.deserializeobject(json);
jarray imgs = (jarray)objroot[imgs];
for (int j = 0 ; j jobject img = (jobject)imgs[j];
string objurl = (string)img[objurl];//http://hibiadu....../1.jpg
// textshow.appendtext(objurl + environment.newline);
//保存的路径是:destdir;
try {
downloadimage(objurl);//避免一个方法中的代码过于复杂
} catch (exception ex) {
//子线程的代码中操作界面控件要使用begininvoke
textshow.begininvoke(new action(() => {
textshow.appendtext(ex.message + environment.newline);
}));
}
}
}
}
private void downloadimage(string objurl) {
//得到保存的路径
string path = path.combine(dir , path.getfilename(objurl));
httpwebrequest req = (httpwebrequest)webrequest.create(objurl);
req.referer = http://image.baidu.com/;//欺骗网站服务器这是从百度图片发出的
using (httpwebresponse res = (httpwebresponse)req.getresponse()) {
if (res.statuscode == httpstatuscode.ok) {
using (stream stream = res.getresponsestream())
using (stream filestream = new filestream(path , filemode.create)) {
stream.copyto(filestream);
}
} else {
throw new exception(下载失败 + res.statuscode);
}
}
}
}
}