Получить данные с сайта - C#

Узнай цену своей работы

Формулировка задачи:

Не получается получить данные с сайта. В ответ на HttpWebRequest приходит
Листинг программы
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html >
  3. <head><title>vicess</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <META HTTP-EQUIV="Cache-Control" CONTENT="max-age=0">
  6. <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
  7. <META http-equiv="expires" content="0">
  8. <META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
  9. <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
  10.  
  11. <link rel="STYLESHEET" type="text/css" href="style2.css">
  12. <script language="JavaScript">
  13.  
  14. //Start phpRequest Object
  15. var autho='';
  16. var SERVER_URL = "server.php";
  17. //Start phpRequest Object
  18. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  19. function encode64(input) {
  20. var output = "";
  21. var chr1, chr2, chr3;
  22. var enc1, enc2, enc3, enc4;
  23. var i = 0;
  24. input=input.toString();
  25. input = utf8_encode(input);
  26. do {
  27. chr1 = input.charCodeAt(i++);
  28. chr2 = input.charCodeAt(i++);
  29. chr3 = input.charCodeAt(i++);
  30. enc1 = chr1 >> 2;
  31. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  32. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  33. enc4 = chr3 & 63;
  34. if (isNaN(chr2)) {
  35. enc3 = enc4 = 64;
  36. } else if (isNaN(chr3)) {
  37. enc4 = 64;
  38. }
  39. output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
  40. keyStr.charAt(enc3) + keyStr.charAt(enc4);
  41. } while (i < input.length);
  42. return output;
  43. }
  44. function decode64(input) {
  45. var output = "";
  46. var chr1, chr2, chr3;
  47. var enc1, enc2, enc3, enc4;
  48. var i = 0;
  49. if(input==''){return '';}
  50. // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  51. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  52. do {
  53. enc1 = keyStr.indexOf(input.charAt(i++));
  54. enc2 = keyStr.indexOf(input.charAt(i++));
  55. enc3 = keyStr.indexOf(input.charAt(i++));
  56. enc4 = keyStr.indexOf(input.charAt(i++));
  57. chr1 = (enc1 << 2) | (enc2 >> 4);
  58. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  59. chr3 = ((enc3 & 3) << 6) | enc4;
  60. output = output + String.fromCharCode(chr1);
  61. if (enc3 != 64) {
  62. output = output + String.fromCharCode(chr2);
  63. }
  64. if (enc4 != 64) {
  65. output = output + String.fromCharCode(chr3);
  66. }
  67. } while (i < input.length);
  68. output = utf8_decode(output);
  69. return output;
  70. }
  71.  
  72. function utf8_encode (string) {
  73. string = string.replace(/\r\n/g,"\n");
  74. var utftext = "";
  75. for (var n = 0; n < string.length; n++) {
  76. var c = string.charCodeAt(n);
  77. if (c < 128) {
  78. utftext += String.fromCharCode(c);
  79. }
  80. else if((c > 127) && (c < 2048)) {
  81. utftext += String.fromCharCode((c >> 6) | 192);
  82. utftext += String.fromCharCode((c & 63) | 128);
  83. }
  84. else {
  85. utftext += String.fromCharCode((c >> 12) | 224);
  86. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  87. utftext += String.fromCharCode((c & 63) | 128);
  88. }
  89. }
  90. return utftext;
  91. }
  92. // private method for UTF-8 decoding
  93. function utf8_decode (utftext) {
  94. var string = "";
  95. var i = 0;
  96. var c = c1 = c2 = 0;
  97. while ( i < utftext.length ) {
  98. c = utftext.charCodeAt(i);
  99. if (c < 128) {
  100. string += String.fromCharCode(c);
  101. i++;
  102. }
  103. else if((c > 191) && (c < 224)) {
  104. c2 = utftext.charCodeAt(i+1);
  105. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  106. i += 2;
  107. }
  108. else {
  109. c2 = utftext.charCodeAt(i+1);
  110. c3 = utftext.charCodeAt(i+2);
  111. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  112. i += 3;
  113. }
  114. }
  115. return string;
  116. }
  117.  
  118. function phpRequest() {
  119. this.parms = new Array();
  120. this.parmsIndex = 0;
  121. this.execute = phpRequestExecute;
  122. this.execute_post=phpRequestExecutePOST;
  123. this.add = phpRequestAdd;
  124. this.server = SERVER_URL;
  125. }
  126. function phpRequestAdd(name,value) {
  127. this.parms[this.parmsIndex] = new Pair(name,value);
  128. this.parmsIndex++;
  129. }
  130. function phpRequestExecute() {
  131. var targetURL = this.server;
  132.  
  133. if(window.XMLHttpRequest) {
  134. try {
  135. httpRequest = new XMLHttpRequest();
  136. } catch(e) {
  137. httpRequest = false;
  138. }
  139. // branch for IE/Windows ActiveX version
  140. } else if(window.ActiveXObject) {
  141. try {
  142. httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  143. } catch(e) {
  144. try {
  145. httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  146. } catch(e) {
  147. httpRequest = false;
  148. }
  149. }
  150. }
  151. try {
  152.  
  153. var txt = '?1';
  154. // var txt = "?1&autho="+autho;
  155. for(var ix in this.parms) {
  156. // var tmp_param=this.parms[ix].value;
  157. // if(this.parms[ix].value != ''){tmp_param = encode64(tmp_param);txt = txt+'&'+this.parms[ix].name+'='+tmp_param;}else{
  158. // txt = txt+'&'+this.parms[ix].name+'='+tmp_param;}
  159. txt = txt+'&'+this.parms[ix].name+'='+this.parms[ix].value;
  160. }
  161. //Two options here, only uncomment one of these
  162. //GET REQUEST
  163. httpRequest.open("GET", targetURL+txt, false, null, null);
  164. //POST REQUEST EXAMPLE
  165. /*
  166. //httpRequest.open("POST", targetURL+txt, false, null, null);
  167. //httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  168. */
  169. httpRequest.send('');
  170. }catch (e){
  171. alert('An error has occured calling the external site: '+e+targetURL+txt);
  172. return false;
  173. }
  174. switch(httpRequest.readyState) {
  175. case 1,2,3:
  176. alert('Bad Ready State: '+httpRequest.status);
  177. return false;
  178. break;
  179. case 4:
  180. if(httpRequest.status !=200) {
  181. alert('The server respond with a bad status code: '+httpRequest.status);
  182. return false;
  183. } else {
  184. var response = httpRequest.responseText;
  185. }
  186. break;
  187. }
  188. return response;
  189. }
  190.  
  191. function phpRequestExecutePOST() {
  192. var targetURL = this.server;
  193.  
  194. if(window.XMLHttpRequest) {
  195. try {
  196. httpRequest = new XMLHttpRequest();
  197. } catch(e) {
  198. httpRequest = false;
  199. }
  200. // branch for IE/Windows ActiveX version
  201. } else if(window.ActiveXObject) {
  202. try {
  203. httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  204. } catch(e) {
  205. try {
  206. httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  207. } catch(e) {
  208. httpRequest = false;
  209. }
  210. }
  211. }
  212. try {
  213. var txt = "autho="+autho+"&uid="+encode64(uid);
  214. // var txt = "?1&autho="+autho;
  215. for(var ix in this.parms) {
  216. // var tmp_param=this.parms[ix].value;
  217. // if(this.parms[ix].value != ''){tmp_param = encode64(tmp_param);txt = txt+'&'+this.parms[ix].name+'='+tmp_param;}else{
  218. // txt = txt+'&'+this.parms[ix].name+'='+tmp_param;}
  219. txt = txt+'&'+this.parms[ix].name+'='+this.parms[ix].value;
  220. }
  221. //Two options here, only uncomment one of these
  222. //GET REQUEST
  223. //httpRequest.open("GET", targetURL, true, null, null);
  224. //POST REQUEST EXAMPLE
  225. httpRequest.open("POST", targetURL,false);
  226. httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  227. httpRequest.setRequestHeader("Content-length", txt.length);
  228. httpRequest.setRequestHeader("Connection", "close");
  229.  
  230. httpRequest.send(txt);
  231. }catch (e){
  232. alert('An error has occured calling the external site: '+e+targetURL+txt);
  233. return false;
  234. }
  235. switch(httpRequest.readyState) {
  236. case 1,2,3:
  237. alert('Bad Ready State: '+httpRequest.status);
  238. return false;
  239. break;
  240. case 4:
  241. if(httpRequest.status !=200) {
  242. alert('The server respond with a bad status code: '+httpRequest.status);
  243. return false;
  244. } else {
  245. var response = httpRequest.responseText;
  246. }
  247. break;
  248. }
  249. return response;
  250. }
  251.  
  252. function Pair(name,value) {
  253. if(value != ''){this.value = encode64(value);}else{
  254. this.value = value;}
  255. this.name = name;
  256. //this.value = value;
  257. }
  258. function checkEan(eanCode) {
  259. // Check if only digits
  260. var ValidChars = "0123456789";
  261. for (i = 0; i < eanCode.length; i++) {
  262. digit = eanCode.charAt(i);
  263. if (ValidChars.indexOf(digit) == -1) {
  264. return false;
  265. }
  266. }
  267. // Add five 0 if the code has only 8 digits
  268. if (eanCode.length == 8 ) {
  269. eanCode = "00000" + eanCode;
  270. }
  271. // Check for 13 digits otherwise
  272. else if (eanCode.length != 13) {
  273. return false;
  274. }
  275. // Get the check number
  276. originalCheck = eanCode.substring(eanCode.length - 1);
  277. eanCode = eanCode.substring(0, eanCode.length - 1);
  278. // Add even numbers together
  279. even = Number(eanCode.charAt(1)) +
  280. Number(eanCode.charAt(3)) +
  281. Number(eanCode.charAt(5)) +
  282. Number(eanCode.charAt(7)) +
  283. Number(eanCode.charAt(9)) +
  284. Number(eanCode.charAt(11));
  285. // Multiply this result by 3
  286. even *= 3;
  287. // Add odd numbers together
  288. odd = Number(eanCode.charAt(0)) +
  289. Number(eanCode.charAt(2)) +
  290. Number(eanCode.charAt(4)) +
  291. Number(eanCode.charAt(6)) +
  292. Number(eanCode.charAt(8)) +
  293. Number(eanCode.charAt(10));
  294. // Add two totals together
  295. total = even + odd;
  296. // Calculate the checksum
  297. // Divide total by 10 and store the remainder
  298. checksum = total % 10;
  299. // If result is not 0 then take away 10
  300. if (checksum != 0) {
  301. checksum = 10 - checksum;
  302. }
  303. // Return the result
  304. if (checksum != originalCheck) {
  305. return false;
  306. }
  307. return true;
  308. }
  309.  
  310. function trim(s)
  311. {
  312. var l=0; var r=s.length -1;
  313. while(l < s.length && s[l] == ' ')
  314. { l++; }
  315. while(r > l && s[r] == ' ')
  316. { r-=1; }
  317. return s.substring(l, r+1);
  318. }
  319.  
  320. sekout=0;
  321. function timeout()
  322. {
  323. setTimeout("timeout_();",2000);
  324.  
  325. }
  326.  
  327. function timeout_(){
  328.  
  329. load();
  330. setTimeout("timeout_();",2000000);
  331. }
  332. function load()
  333. {
  334. praca="204";
  335. tmp_html='<table>';
  336. tmp_html2='<table>';
  337. tmp_html3='<table>';
  338. tmp_html4='<table>';
  339. req = new phpRequest();
  340. req.add('praca',praca);
  341. var response = req.execute();
  342.  
  343. response=response.split('#');
  344. i=0;
  345. j=0;
  346. while(response[i]!=''){
  347. res=response[i].split('&');
  348. for(var tid=0; tid<res.length; tid++){if(res[tid] != ''){res[tid]=decode64(res[tid]);}}
  349. res[0]++;
  350. if(res[0]>10 && res[0]<20){
  351. tmp_html+='<tr><td>'+res[1]+'</td><td>'+res[2]+'</td></tr>';
  352. }
  353. if(res[0]>20 && res[0]<30){
  354. tmp_html2+='<tr><td>'+res[1]+'</td><td>'+res[2]+'</td></tr>';
  355. }
  356. if(res[0]>30 && res[0]<40){
  357. tmp_html3+='<tr><td>'+res[1]+'</td><td>'+res[2]+'</td></tr>';
  358. }
  359. if(res[0]>40 && res[0]<50){
  360. tmp_html4+='<tr><td>'+res[1]+'</td><td>'+res[2]+'</td></tr>';
  361. }
  362. j++;
  363. i++;
  364. }
  365.  
  366. document.getElementById('messageTree').innerHTML=tmp_html+'</table>';
  367. document.getElementById('messageTree2').innerHTML=tmp_html2+'</table>';
  368. document.getElementById('messageTree3').innerHTML=tmp_html3+'</table>';
  369. document.getElementById('messageTree4').innerHTML=tmp_html4+'</table>';
  370.  
  371. }
  372.  
  373. </script>
  374. </head>
  375. <body style="width: 1024px; background:black;-moz-transform:scale(0.65);-moz-transform-origin:0 0;" scroll="no" onload="timeout();" class="messageTreeBody" align="center" >
  376.  
  377. <table width="100%" ><tr><td>
  378. <table ><td valign=top style="color:white;font-size:15px" id="messageTree">
  379. </td>
  380. <td valign=top style="color:white;font-size:15px" id="messageTree2">
  381. </td>
  382. <td valign=top style="color:white;font-size:15px" id="messageTree3">
  383. </td>
  384. <td valign=top style="color:white;font-size:15px" id="messageTree4">
  385. </td>
  386. <td valign=top style="color:white;font-size:15px" id="messageTree5">
  387. </td></tr>
  388. </table>
  389. </body></html>
Но нужных данных в таблице нет.

Решение задачи: «Получить данные с сайта»

textual
Листинг программы
  1. WebBrowser webBrowser1 = new WebBrowser();
  2. webBrowser1.Navigate("URL");
  3. webBrowser1.ScriptErrorsSuppressed = true;
  4. while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
  5. {
  6.     Application.DoEvents();
  7. }
  8. string to_write = webBrowser1.DocumentText;
  9. FileStream afile = new FileStream("file_path", FileMode.OpenOrCreate);
  10. StreamWriter sw = new StreamWriter(afile);
  11. sw.Write(to_write);
  12. sw.Close();

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 3.714 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут