public static string loginIn() { try { XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(Application.StartupPath + "/LoginConfig.xml"); XmlNode address = xmldoc.SelectSingleNode("//address"); XmlNode account = xmldoc.SelectSingleNode("//account"); XmlNode password = xmldoc.SelectSingleNode("//password"); HttpHelper httphelper = new HttpHelper(); NameValueCollection namevaluec = new NameValueCollection(); namevaluec.Add("token", "login"); namevaluec.Add("username", account.InnerText); namevaluec.Add("password", password.InnerText); namevaluec.Add("version", Application.ProductVersion); string loginrequest = httphelper.postHttpRequest("http://" + address + "/api/User/post", namevaluec); return loginrequest; } catch (Exception ex) { MessageBox.Show(ex.Message); return ""; } }
private void InitializeForm() { XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(Application.StartupPath + "/LoginConfig.xml"); XmlNode address = xmldoc.SelectSingleNode("//address"); //XmlNodeList xnlist = xmldoc.SelectNodes("//Template"); //List<string> classiten = new List<string>(); //for (int i = 0; i < xnlist.Count; i++) //{ // XmlElement xmle = (XmlElement)xnlist[i]; // //string id = xmle.GetAttribute("id"); // string text = xmle.GetAttribute("text"); // this.toolStripComboBoxClass.Items.Add(text); // //classiten.Add(text); //} try { HttpHelper httphelper = new HttpHelper(); string stagexml = httphelper.getHttpRespone("http://" + address.InnerText + "/api/Item/get?token=stage"); XmlDocument xmldocstage = new XmlDocument(); xmldocstage.LoadXml(stagexml.Trim()); xmldocstage.Save(Application.StartupPath + stagefilename); string sourcexml = httphelper.getHttpRespone("http://" + address.InnerText + "/api/Item/get?token=source"); XmlDocument xmldocsource = new XmlDocument(); xmldocsource.LoadXml(sourcexml.Trim()); xmldocsource.Save(Application.StartupPath + sourcefilename); string xuekexml = httphelper.getHttpRespone("http://" + address.InnerText + "/api/Item/get?token=xueke"); XmlDocument xmldocxueke = new XmlDocument(); xmldocxueke.LoadXml(xuekexml.Trim()); xmldocxueke.Save(Application.StartupPath + xuekefilename); } catch(Exception ex) { MessageBox.Show(ex.Message); } testpaper = new TestPaper();//初始化试卷属性 foreach (string st in Enum.GetNames(typeof(Subject.SubjecttypeEnum))) { System.Windows.Forms.TreeNode treeNode = new System.Windows.Forms.TreeNode(st); treeNode.Text = st; this.treeViewSubject.Nodes.Add(treeNode); } }
/// <summary> /// 压缩并上传拆分后的题目 /// </summary> /// <param name="onlyZip"></param> private void zipFileAndUpload(bool onlyZip) { try { if (this.mainContent1.WordBrowers.document == null) { MessageBox.Show("请先在程序中打开word文件。"); return; } if ( testpaper.学科 == null || testpaper.学科.Equals("")) { MessageBox.Show("请先填写完整试卷信息。"); return; } //if (!Regex.IsMatch(testpaper.年份, @"^\d{4}$")) //{ // MessageBox.Show("年份只能为4位数字。"); // return; //} if (!onlyZip && MessageBox.Show("确定开始上传试卷吗?", "提示", MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } Paper root = new Paper(); for (int i = 0; i < this.treeViewSubject.Nodes.Count; i++) { SubjectItem subjectItem = new SubjectItem(); createSubjectItem(this.treeViewSubject.Nodes[i], this.treeViewSubject.Nodes[i].Text, ref subjectItem); root.bigSubject.Add(subjectItem); } if (root.bigSubject.Count > 0) { if (root.bigSubject[0].subSubjectItemList.Count == 0) { MessageBox.Show("文件未被识别无法打包上传。"); return; } } //root.source = testpaper.来源; //root.stage = testpaper.阶段; //XmlDocument xmldocstage = new XmlDocument(); //xmldocstage.Load(Application.StartupPath + MartianSimple.stagefilename); //XmlNodeList xnlstage = xmldocstage.SelectNodes("//Result"); //for (int i = 0; i < xnlstage.Count; i++) //{ // XmlElement xmlstage = (XmlElement)xnlstage[i]; // string str = xmlstage.GetAttribute("Id"); // if (testpaper.阶段.Equals(str)) // { // root.stage = (i + 1).ToString(); // break; // } //} //root.xueke = testpaper.学科; XmlDocument xmldocclass = new XmlDocument(); xmldocclass.Load(Application.StartupPath + MartianSimple.xuekefilename); XmlNodeList xnl = xmldocclass.SelectNodes("//Id"); XmlNodeList xnltitle = xmldocclass.SelectNodes("//Title"); for (int i = 0; i < xnl.Count; i++) { //XmlNode xmlrr = xnl[i]; //XmlNode stagexmode = xmlrr.SelectSingleNode("//Title"); string strp = xnltitle[i].InnerText; string strb = testpaper.学科; if (strp.Equals(strb)) { //XmlNode stagexmodeId = xnl[i].SelectSingleNode("//Id"); root.xueke = xnl[i].InnerText; break; } } //root.year = testpaper.年份; root.tag = testpaper.标签; root.papername = testpaper.试卷名称; XmlSerializer xmls = new XmlSerializer(typeof(Paper)); StringWriter sw = new StringWriter(); xmls.Serialize(sw, root); XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(sw.ToString()); string xmlpath = System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile + "uploadfile.xml"; if (!Directory.Exists(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile)) { Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile); } xmldoc.Save(xmlpath); //压缩上传内容 ZipHelper.CreateZip(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile, System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip"); //Directory.Delete(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile, true); //上传文件内容 if (!onlyZip) { XmlDocument xmldocc = new XmlDocument(); xmldoc.Load(Application.StartupPath + "/LoginConfig.xml"); XmlNode address = xmldoc.SelectSingleNode("//address"); XmlNode account = xmldoc.SelectSingleNode("//account"); XmlNode password = xmldoc.SelectSingleNode("//password"); //使用webservice上传 //Model_WebServicePortClient client = new Model_WebServicePortClient("Model_WebServicePort", address.InnerText); //byte[] buffer = File.ReadAllBytes(System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip"); ////StreamReader sr=new StreamReader(System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip"); ////sr.Read(buffer,0,sr); //string base64String = System.Convert.ToBase64String(buffer, 0, buffer.Length); //client.itemupload("uploadfile.zip", base64String); //client.ClientCredentials.UserName.UserName = account.InnerText; //client.ClientCredentials.UserName.Password = password.InnerText; //使用resetful上传 HttpHelper httphelper = new HttpHelper(); NameValueCollection namevaluec = new NameValueCollection(); namevaluec.Add("token", "apiupload"); //namevaluec.Add("xueke", this.testpaper.学科); namevaluec.Add("username", account.InnerText); string loginrequest = httphelper.postFile("http://" + address.InnerText + "/api/Item/post", System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip", namevaluec); //string loginrequest = httphelper.Upload_Request("http://*****:*****@"\uploadfile.zip", System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip", namevaluec); //string loginrequest = httphelper.UploadFileEx("http://" + address.InnerText + "/api/Item/post", System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip", null, null, namevaluec, null);//http://" + address.InnerText + "/api/Item/post XmlDocument docrequest = new XmlDocument(); docrequest.LoadXml(loginrequest.Trim()); XmlNode statusnode = docrequest.SelectSingleNode("//Status"); if (statusnode.InnerText.Equals("fail")) { XmlNode errormessagenode = docrequest.SelectSingleNode("//ErrorMessage"); MessageBox.Show(errormessagenode.InnerText); } else { MessageBox.Show("上传成功"); //if (Directory.Exists(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile)) // Directory.Delete(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile, true); } } // File.Delete(System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip"); } catch (DirectoryNotFoundException ex) { MessageBox.Show("上传失败," + ex.Message); } catch (XmlException xmle) { MessageBox.Show("上传失败,请检查服务器地址设置是否正确。"); } catch (Exception ex) { MessageBox.Show("上传失败,"+ex.Message); } }
private void uploadZipToolStripMenuItem_Click(object sender, EventArgs e) { try { if (openFileDialog2.ShowDialog() == DialogResult.OK) { XmlDocument xmldocc = new XmlDocument(); xmldocc.Load(Application.StartupPath + "/LoginConfig.xml"); XmlNode address = xmldocc.SelectSingleNode("//address"); XmlNode account = xmldocc.SelectSingleNode("//account"); HttpHelper httphelper = new HttpHelper(); NameValueCollection namevaluec = new NameValueCollection(); namevaluec.Add("token", "apiupload"); namevaluec.Add("xueke", this.toolStripComboBoxClass.Text); namevaluec.Add("username", account.InnerText); string loginrequest = httphelper.postFile("http://" + address.InnerText + "/api/Item/post", openFileDialog2.FileName, namevaluec); //string loginrequest = httphelper.postFile("http://*****:*****@"\uploadfile.zip", System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip", namevaluec); //string loginrequest = httphelper.UploadFileEx("http://" + address.InnerText + "/api/Item/post", System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip", null, null, namevaluec, null);//http://" + address.InnerText + "/api/Item/post XmlDocument docrequest = new XmlDocument(); docrequest.LoadXml(loginrequest.Trim()); XmlNode statusnode = docrequest.SelectSingleNode("//Status"); if (statusnode.InnerText.Equals("fail")) { XmlNode errormessagenode = docrequest.SelectSingleNode("//ErrorMessage"); MessageBox.Show(errormessagenode.InnerText, "提示"); } else { MessageBox.Show("上传成功", "提示"); } } } catch (DirectoryNotFoundException ex) { MessageBox.Show("上传失败," + ex.Message, "错误"); } catch (XmlException xmle) { MessageBox.Show("上传失败,请检查服务器地址设置是否正确。", "错误"); } catch (Exception ex) { MessageBox.Show("上传失败," + ex.Message, "错误"); } }
/// <summary> /// 上传试卷内容 /// </summary> private void UploadPaperZip() { XmlDocument xmldocc = new XmlDocument(); xmldocc.Load(Application.StartupPath + "/LoginConfig.xml"); XmlNode address = xmldocc.SelectSingleNode("//address"); XmlNode token = xmldocc.SelectSingleNode("//token"); XmlNode account = xmldocc.SelectSingleNode("//account"); XmlNode password = xmldocc.SelectSingleNode("//password"); HttpHelper httphelper = new HttpHelper(); NameValueCollection namevaluec = new NameValueCollection(); namevaluec.Add("token", token.InnerText); namevaluec.Add("xueke", this.toolStripComboBoxClass.Text); namevaluec.Add("username", account.InnerText); string loginrequest = httphelper.postFile("http://" + address.InnerText + "/paperapi/index", System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip", namevaluec); //string loginrequest = httphelper.Upload_Request("http://*****:*****@"\uploadfile.zip", System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip", namevaluec); //string loginrequest = httphelper.UploadFileEx("http://" + address.InnerText + "/api/Item/post", System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip", null, null, namevaluec, null);//http://" + address.InnerText + "/api/Item/post XmlDocument docrequest = new XmlDocument(); docrequest.LoadXml(loginrequest.Trim()); XmlNode statusnode = docrequest.SelectSingleNode("//Status"); if (statusnode.InnerText.Equals("fail")) { XmlNode errormessagenode = docrequest.SelectSingleNode("//ErrorMessage"); MessageBox.Show(errormessagenode.InnerText, "提示"); } else { MessageBox.Show("上传成功", "提示"); } }
/// <summary> /// 上传图片附件 /// </summary> private void UploadImages() { uploadimgsdic.Clear(); XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(Application.StartupPath + "/LoginConfig.xml"); XmlNode uploadimgurl = xmldoc.SelectSingleNode("//uploadimgurl"); String upload = uploadimgurl.InnerText; //String upload = "http://192.168.1.104:3635/wiki/daoti2/"; HttpHelper httphelper = new HttpHelper(); NameValueCollection namevaluec = new NameValueCollection(); DirectoryInfo di = new DirectoryInfo(System.Windows.Forms.Application.StartupPath + "/uploadfile"); try { FileInfo[] fileInfo = di.GetFiles(); int i = 1; foreach (FileInfo subinfo in fileInfo) { string uoloadimgresp = httphelper.postFile(upload, subinfo.FullName, namevaluec); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(uoloadimgresp))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UploadImgResp)); UploadImgResp uploadimgresp = (UploadImgResp)serializer.ReadObject(ms); if (uploadimgresp.state.Equals("SUCCESS")) { uploadimgsdic.Add(subinfo.Name, uploadimgresp.url); } } float value = ((float)i++ / (float)fileInfo.Length) * 90; this.progressBarSpot.Value = (int)value; Thread.Sleep(50); //MessageBox.Show(uoloadimgresp); } } catch (DirectoryNotFoundException dmfe) { } isUploadFinish = true; }
/// <summary> /// 提交试卷内容 /// </summary> private void PostPaper(string paper) { XmlDocument xmldocc = new XmlDocument(); xmldocc.Load(Application.StartupPath + "/LoginConfig.xml"); XmlNode access_token = xmldocc.SelectSingleNode("//access_token"); XmlNode refresh_token = xmldocc.SelectSingleNode("//refresh_token"); if (string.IsNullOrEmpty(access_token.InnerText) || string.IsNullOrEmpty(refresh_token.InnerText)) { AccountManage pm = new AccountManage(); DialogResult dr= pm.ShowDialog(); if (dr.Equals(DialogResult.Cancel)) { return; } xmldocc.Load(Application.StartupPath + "/LoginConfig.xml"); access_token = xmldocc.SelectSingleNode("//access_token"); refresh_token = xmldocc.SelectSingleNode("//refresh_token"); } XmlNode address = xmldocc.SelectSingleNode("//address"); XmlNode account = xmldocc.SelectSingleNode("//account"); XmlNode password = xmldocc.SelectSingleNode("//password"); XmlNode token_type = xmldocc.SelectSingleNode("//token_type"); XmlNode openid = xmldocc.SelectSingleNode("//openid"); XmlNode postpaper = xmldocc.SelectSingleNode("//postpaper"); HttpHelper httphelper = new HttpHelper(); NameValueCollection namevaluec = new NameValueCollection(); namevaluec.Add("access_token", access_token.InnerText); namevaluec.Add("refresh_token", refresh_token.InnerText); namevaluec.Add("token_type", token_type.InnerText); namevaluec.Add("openid", openid.InnerText); namevaluec.Add("userid", account.InnerText); namevaluec.Add("client_id", "100006"); namevaluec.Add("clientip", "127.0.0.1"); //namevaluec.Add("oauth_version", "2.a"); namevaluec.Add("paper", System.Web.HttpUtility.UrlEncode(paper, System.Text.Encoding.UTF8)); string postpaperrequest = httphelper.postHttpRequest(postpaper.InnerText, namevaluec); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(postpaperrequest))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UploadImgResp)); UploadImgResp uploadimgresp = (UploadImgResp)serializer.ReadObject(ms); if (uploadimgresp.ret.Equals("1")) { MessageBox.Show(uploadimgresp.msg, "提示"); } else { this.progressBarSpot.Value = 100; this.progressBarSpot.Visible = false; UploadSuccess uploadSuccess = new UploadSuccess(); uploadSuccess.ExamId = uploadimgresp.data.examId; uploadSuccess.ShowDialog(); } } }
private void button1_Click(object sender, EventArgs e) { // NameValueCollection nc = (NameValueCollection)ConfigurationSettings.GetConfig("name"); //String name = ConfigurationSettings.AppSettings["nameaaa"]; //string name = (string)Properties.Settings.Default["name"]; //StringCollection subjectPre = (StringCollection)Properties.Settings.Default["subjectPre"]; //this.textBoxAccount.Text = subjectPre[0]; //this.textBoxPassword.Text = subjectPre[1]; string passwordmd5 = ""; try { XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(Application.StartupPath + "/LoginConfig.xml"); XmlNode address = xmldoc.SelectSingleNode("//address"); XmlNode account = xmldoc.SelectSingleNode("//account"); XmlNode password = xmldoc.SelectSingleNode("//password"); XmlNode token = xmldoc.SelectSingleNode("//token"); XmlNode isLoginDefault = xmldoc.SelectSingleNode("//isLoginDefault"); if (this.checkBox1.Checked) { address.InnerText = this.textBoxAddress.Text; account.InnerText = this.textBoxAccount.Text.Trim(); MD5 md5 = new MD5CryptoServiceProvider(); byte[] bt = md5.ComputeHash(UnicodeEncoding.UTF8.GetBytes(this.textBoxPassword.Text)); if (password.InnerText.Equals(this.textBoxPassword.Text)) { passwordmd5 = this.textBoxPassword.Text; } else { password.InnerText = BitConverter.ToString(bt).Replace("-", ""); passwordmd5 = password.InnerText; } isLoginDefault.InnerText = "true"; } else { address.InnerText = this.textBoxAddress.Text; account.InnerText = this.textBoxAccount.Text.Trim(); password.InnerText = ""; isLoginDefault.InnerText = "false"; } xmldoc.Save(Application.StartupPath + "/LoginConfig.xml"); } catch (Exception ex) { MessageBox.Show("配置文件出错.请重新安装程序修复", "提示"); } try { HttpHelper httphelper = new HttpHelper(); NameValueCollection namevaluec = new NameValueCollection(); namevaluec.Add("client_id", "100006"); namevaluec.Add("username", this.textBoxAccount.Text.Trim()); namevaluec.Add("password", this.textBoxPassword.Text); namevaluec.Add("grant_type", "password"); string loginrequest = httphelper.postHttpRequest("http://" + this.textBoxAddress.Text + "/oauth/token", namevaluec); LoginjJsonResult(loginrequest); //LoginXmlResult(loginrequest); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误"); } }