Получить данные с сайта - C#
Формулировка задачи:
Не получается получить данные с сайта. В ответ на HttpWebRequest приходит
Но нужных данных в таблице нет.
Листинг программы
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html >
- <head><title>vicess</title>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <META HTTP-EQUIV="Cache-Control" CONTENT="max-age=0">
- <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
- <META http-equiv="expires" content="0">
- <META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
- <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
- <link rel="STYLESHEET" type="text/css" href="style2.css">
- <script language="JavaScript">
- //Start phpRequest Object
- var autho='';
- var SERVER_URL = "server.php";
- //Start phpRequest Object
- var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
- function encode64(input) {
- var output = "";
- var chr1, chr2, chr3;
- var enc1, enc2, enc3, enc4;
- var i = 0;
- input=input.toString();
- input = utf8_encode(input);
- do {
- chr1 = input.charCodeAt(i++);
- chr2 = input.charCodeAt(i++);
- chr3 = input.charCodeAt(i++);
- enc1 = chr1 >> 2;
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
- enc4 = chr3 & 63;
- if (isNaN(chr2)) {
- enc3 = enc4 = 64;
- } else if (isNaN(chr3)) {
- enc4 = 64;
- }
- output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
- keyStr.charAt(enc3) + keyStr.charAt(enc4);
- } while (i < input.length);
- return output;
- }
- function decode64(input) {
- var output = "";
- var chr1, chr2, chr3;
- var enc1, enc2, enc3, enc4;
- var i = 0;
- if(input==''){return '';}
- // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
- input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
- do {
- enc1 = keyStr.indexOf(input.charAt(i++));
- enc2 = keyStr.indexOf(input.charAt(i++));
- enc3 = keyStr.indexOf(input.charAt(i++));
- enc4 = keyStr.indexOf(input.charAt(i++));
- chr1 = (enc1 << 2) | (enc2 >> 4);
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
- chr3 = ((enc3 & 3) << 6) | enc4;
- output = output + String.fromCharCode(chr1);
- if (enc3 != 64) {
- output = output + String.fromCharCode(chr2);
- }
- if (enc4 != 64) {
- output = output + String.fromCharCode(chr3);
- }
- } while (i < input.length);
- output = utf8_decode(output);
- return output;
- }
- function utf8_encode (string) {
- string = string.replace(/\r\n/g,"\n");
- var utftext = "";
- for (var n = 0; n < string.length; n++) {
- var c = string.charCodeAt(n);
- if (c < 128) {
- utftext += String.fromCharCode(c);
- }
- else if((c > 127) && (c < 2048)) {
- utftext += String.fromCharCode((c >> 6) | 192);
- utftext += String.fromCharCode((c & 63) | 128);
- }
- else {
- utftext += String.fromCharCode((c >> 12) | 224);
- utftext += String.fromCharCode(((c >> 6) & 63) | 128);
- utftext += String.fromCharCode((c & 63) | 128);
- }
- }
- return utftext;
- }
- // private method for UTF-8 decoding
- function utf8_decode (utftext) {
- var string = "";
- var i = 0;
- var c = c1 = c2 = 0;
- while ( i < utftext.length ) {
- c = utftext.charCodeAt(i);
- if (c < 128) {
- string += String.fromCharCode(c);
- i++;
- }
- else if((c > 191) && (c < 224)) {
- c2 = utftext.charCodeAt(i+1);
- string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
- i += 2;
- }
- else {
- c2 = utftext.charCodeAt(i+1);
- c3 = utftext.charCodeAt(i+2);
- string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
- i += 3;
- }
- }
- return string;
- }
- function phpRequest() {
- this.parms = new Array();
- this.parmsIndex = 0;
- this.execute = phpRequestExecute;
- this.execute_post=phpRequestExecutePOST;
- this.add = phpRequestAdd;
- this.server = SERVER_URL;
- }
- function phpRequestAdd(name,value) {
- this.parms[this.parmsIndex] = new Pair(name,value);
- this.parmsIndex++;
- }
- function phpRequestExecute() {
- var targetURL = this.server;
- if(window.XMLHttpRequest) {
- try {
- httpRequest = new XMLHttpRequest();
- } catch(e) {
- httpRequest = false;
- }
- // branch for IE/Windows ActiveX version
- } else if(window.ActiveXObject) {
- try {
- httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
- } catch(e) {
- try {
- httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
- } catch(e) {
- httpRequest = false;
- }
- }
- }
- try {
- var txt = '?1';
- // var txt = "?1&autho="+autho;
- for(var ix in this.parms) {
- // var tmp_param=this.parms[ix].value;
- // if(this.parms[ix].value != ''){tmp_param = encode64(tmp_param);txt = txt+'&'+this.parms[ix].name+'='+tmp_param;}else{
- // txt = txt+'&'+this.parms[ix].name+'='+tmp_param;}
- txt = txt+'&'+this.parms[ix].name+'='+this.parms[ix].value;
- }
- //Two options here, only uncomment one of these
- //GET REQUEST
- httpRequest.open("GET", targetURL+txt, false, null, null);
- //POST REQUEST EXAMPLE
- /*
- //httpRequest.open("POST", targetURL+txt, false, null, null);
- //httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
- */
- httpRequest.send('');
- }catch (e){
- alert('An error has occured calling the external site: '+e+targetURL+txt);
- return false;
- }
- switch(httpRequest.readyState) {
- case 1,2,3:
- alert('Bad Ready State: '+httpRequest.status);
- return false;
- break;
- case 4:
- if(httpRequest.status !=200) {
- alert('The server respond with a bad status code: '+httpRequest.status);
- return false;
- } else {
- var response = httpRequest.responseText;
- }
- break;
- }
- return response;
- }
- function phpRequestExecutePOST() {
- var targetURL = this.server;
- if(window.XMLHttpRequest) {
- try {
- httpRequest = new XMLHttpRequest();
- } catch(e) {
- httpRequest = false;
- }
- // branch for IE/Windows ActiveX version
- } else if(window.ActiveXObject) {
- try {
- httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
- } catch(e) {
- try {
- httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
- } catch(e) {
- httpRequest = false;
- }
- }
- }
- try {
- var txt = "autho="+autho+"&uid="+encode64(uid);
- // var txt = "?1&autho="+autho;
- for(var ix in this.parms) {
- // var tmp_param=this.parms[ix].value;
- // if(this.parms[ix].value != ''){tmp_param = encode64(tmp_param);txt = txt+'&'+this.parms[ix].name+'='+tmp_param;}else{
- // txt = txt+'&'+this.parms[ix].name+'='+tmp_param;}
- txt = txt+'&'+this.parms[ix].name+'='+this.parms[ix].value;
- }
- //Two options here, only uncomment one of these
- //GET REQUEST
- //httpRequest.open("GET", targetURL, true, null, null);
- //POST REQUEST EXAMPLE
- httpRequest.open("POST", targetURL,false);
- httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
- httpRequest.setRequestHeader("Content-length", txt.length);
- httpRequest.setRequestHeader("Connection", "close");
- httpRequest.send(txt);
- }catch (e){
- alert('An error has occured calling the external site: '+e+targetURL+txt);
- return false;
- }
- switch(httpRequest.readyState) {
- case 1,2,3:
- alert('Bad Ready State: '+httpRequest.status);
- return false;
- break;
- case 4:
- if(httpRequest.status !=200) {
- alert('The server respond with a bad status code: '+httpRequest.status);
- return false;
- } else {
- var response = httpRequest.responseText;
- }
- break;
- }
- return response;
- }
- function Pair(name,value) {
- if(value != ''){this.value = encode64(value);}else{
- this.value = value;}
- this.name = name;
- //this.value = value;
- }
- function checkEan(eanCode) {
- // Check if only digits
- var ValidChars = "0123456789";
- for (i = 0; i < eanCode.length; i++) {
- digit = eanCode.charAt(i);
- if (ValidChars.indexOf(digit) == -1) {
- return false;
- }
- }
- // Add five 0 if the code has only 8 digits
- if (eanCode.length == 8 ) {
- eanCode = "00000" + eanCode;
- }
- // Check for 13 digits otherwise
- else if (eanCode.length != 13) {
- return false;
- }
- // Get the check number
- originalCheck = eanCode.substring(eanCode.length - 1);
- eanCode = eanCode.substring(0, eanCode.length - 1);
- // Add even numbers together
- even = Number(eanCode.charAt(1)) +
- Number(eanCode.charAt(3)) +
- Number(eanCode.charAt(5)) +
- Number(eanCode.charAt(7)) +
- Number(eanCode.charAt(9)) +
- Number(eanCode.charAt(11));
- // Multiply this result by 3
- even *= 3;
- // Add odd numbers together
- odd = Number(eanCode.charAt(0)) +
- Number(eanCode.charAt(2)) +
- Number(eanCode.charAt(4)) +
- Number(eanCode.charAt(6)) +
- Number(eanCode.charAt(8)) +
- Number(eanCode.charAt(10));
- // Add two totals together
- total = even + odd;
- // Calculate the checksum
- // Divide total by 10 and store the remainder
- checksum = total % 10;
- // If result is not 0 then take away 10
- if (checksum != 0) {
- checksum = 10 - checksum;
- }
- // Return the result
- if (checksum != originalCheck) {
- return false;
- }
- return true;
- }
- function trim(s)
- {
- var l=0; var r=s.length -1;
- while(l < s.length && s[l] == ' ')
- { l++; }
- while(r > l && s[r] == ' ')
- { r-=1; }
- return s.substring(l, r+1);
- }
- sekout=0;
- function timeout()
- {
- setTimeout("timeout_();",2000);
- }
- function timeout_(){
- load();
- setTimeout("timeout_();",2000000);
- }
- function load()
- {
- praca="204";
- tmp_html='<table>';
- tmp_html2='<table>';
- tmp_html3='<table>';
- tmp_html4='<table>';
- req = new phpRequest();
- req.add('praca',praca);
- var response = req.execute();
- response=response.split('#');
- i=0;
- j=0;
- while(response[i]!=''){
- res=response[i].split('&');
- for(var tid=0; tid<res.length; tid++){if(res[tid] != ''){res[tid]=decode64(res[tid]);}}
- res[0]++;
- if(res[0]>10 && res[0]<20){
- tmp_html+='<tr><td>'+res[1]+'</td><td>'+res[2]+'</td></tr>';
- }
- if(res[0]>20 && res[0]<30){
- tmp_html2+='<tr><td>'+res[1]+'</td><td>'+res[2]+'</td></tr>';
- }
- if(res[0]>30 && res[0]<40){
- tmp_html3+='<tr><td>'+res[1]+'</td><td>'+res[2]+'</td></tr>';
- }
- if(res[0]>40 && res[0]<50){
- tmp_html4+='<tr><td>'+res[1]+'</td><td>'+res[2]+'</td></tr>';
- }
- j++;
- i++;
- }
- document.getElementById('messageTree').innerHTML=tmp_html+'</table>';
- document.getElementById('messageTree2').innerHTML=tmp_html2+'</table>';
- document.getElementById('messageTree3').innerHTML=tmp_html3+'</table>';
- document.getElementById('messageTree4').innerHTML=tmp_html4+'</table>';
- }
- </script>
- </head>
- <body style="width: 1024px; background:black;-moz-transform:scale(0.65);-moz-transform-origin:0 0;" scroll="no" onload="timeout();" class="messageTreeBody" align="center" >
- <table width="100%" ><tr><td>
- <table ><td valign=top style="color:white;font-size:15px" id="messageTree">
- </td>
- <td valign=top style="color:white;font-size:15px" id="messageTree2">
- </td>
- <td valign=top style="color:white;font-size:15px" id="messageTree3">
- </td>
- <td valign=top style="color:white;font-size:15px" id="messageTree4">
- </td>
- <td valign=top style="color:white;font-size:15px" id="messageTree5">
- </td></tr>
- </table>
- </body></html>
Решение задачи: «Получить данные с сайта»
textual
Листинг программы
- WebBrowser webBrowser1 = new WebBrowser();
- webBrowser1.Navigate("URL");
- webBrowser1.ScriptErrorsSuppressed = true;
- while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
- {
- Application.DoEvents();
- }
- string to_write = webBrowser1.DocumentText;
- FileStream afile = new FileStream("file_path", FileMode.OpenOrCreate);
- StreamWriter sw = new StreamWriter(afile);
- sw.Write(to_write);
- sw.Close();
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д