problem with http request

M

myhyli

I want to get some files from another remote server,so:

1.at first, I use xmlhttp(activeXObject),but I found it will not work with
some url like: http://rss.xinhuanet.com/rss/it.xml , and some url can,like:
http://rss.chinabyte.com/216454257090494464.xml (BUT I can access those url
correctly by IE)

I don't know why,and I don't think anything wrong with the program
so I decide to use another method

2.I use .net WebRequest class to do the same thing,but I got the same
result: failed with some url

what can I do?so at last, I think I have no choise but to use sockets

3.I use .net System.Net.Sockets to create an TcpClient to connect the the
HTTP server. This time I failed with any url :( . the remote http server
returned "400 bad request". I'm so unlucky!

the code here:
////////////////////////////////////////////////////////////////////////////
//////
<%@ page language="JScript" Debug="true" contenttype="text/plain"%>
<%@ Import Namespace="System.Net.Sockets" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Object" %>
<%
var server = "www.blogchina.com";
var file = "/xml/1_rss2.xml";
var port = "80";

var outputStr = [];
var cmdMultiLine = [];

var t = new Date();

init();

function init(){

//connect to HTTP server
var tcpClient = new TcpClient();
tcpClient.Connect(server, port);

//get stream
var netStream = tcpClient.GetStream();
var readStream = new StreamReader(netStream,Encoding.GetEncoding(936));

cmdMultiLine[cmdMultiLine.length] = "GET /xml/1_rss2.xml HTTP/1.0";
cmdMultiLine[cmdMultiLine.length] = "Accept: */*";
cmdMultiLine[cmdMultiLine.length] = "Accept-Language: zh-cn";
cmdMultiLine[cmdMultiLine.length] = "Accept-Encoding: gzip, deflate";
cmdMultiLine[cmdMultiLine.length] = "User-Agent: Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.0; MyIE2)";
cmdMultiLine[cmdMultiLine.length] = "Host: www.blogchina.com";
cmdMultiLine[cmdMultiLine.length] = "Connection: Keep-Alive";
cmdMultiLine[cmdMultiLine.length] = "Cache-Control: no-cache";
cmdMultiLine[cmdMultiLine.length] = "Cookie:
ASPSESSIONIDCSDBRDBB=BEOEIOCAGCMBKIHMDFOKIGLI\r\n\r\n";

var cmd =
Encoding.GetEncoding("ascii").GetBytes(cmdMultiLine.join("\r\n").ToCharArray
());
netStream.Write(cmd,0,cmd.Length);

var read = new Char[256];
var count = readStream.Read( read, 0, 256 );
while (count > 0){
var str = new System.String(read, 0, count);
outputStr[outputStr.length] = str;
count = readStream.Read(read, 0, 256);
}

netStream.Close();
tcpClient.Close();
}

%>

<% = outputStr.join("\r\n") %>
<!--<%=(new Date()-t)%>-->

////////////////////////////////////////////////////////////////////////////
//

thanks in advance
 
S

Scott Allen

Hi myhyli:

If you can show us the code you have for using the WebRequest, I'm
sure we can help out with that class. Should be possible and easier
than using sockets.

--
Scott
http://www.OdeToCode.com/

I want to get some files from another remote server,so:

1.at first, I use xmlhttp(activeXObject),but I found it will not work with
some url like: http://rss.xinhuanet.com/rss/it.xml , and some url can,like:
http://rss.chinabyte.com/216454257090494464.xml (BUT I can access those url
correctly by IE)

I don't know why,and I don't think anything wrong with the program
so I decide to use another method

2.I use .net WebRequest class to do the same thing,but I got the same
result: failed with some url

what can I do?so at last, I think I have no choise but to use sockets

3.I use .net System.Net.Sockets to create an TcpClient to connect the the
HTTP server. This time I failed with any url :( . the remote http server
returned "400 bad request". I'm so unlucky!

the code here:
////////////////////////////////////////////////////////////////////////////
//////
<%@ page language="JScript" Debug="true" contenttype="text/plain"%>
<%@ Import Namespace="System.Net.Sockets" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Object" %>
<%
var server = "www.blogchina.com";
var file = "/xml/1_rss2.xml";
var port = "80";

var outputStr = [];
var cmdMultiLine = [];

var t = new Date();

init();

function init(){

//connect to HTTP server
var tcpClient = new TcpClient();
tcpClient.Connect(server, port);

//get stream
var netStream = tcpClient.GetStream();
var readStream = new StreamReader(netStream,Encoding.GetEncoding(936));

cmdMultiLine[cmdMultiLine.length] = "GET /xml/1_rss2.xml HTTP/1.0";
cmdMultiLine[cmdMultiLine.length] = "Accept: */*";
cmdMultiLine[cmdMultiLine.length] = "Accept-Language: zh-cn";
cmdMultiLine[cmdMultiLine.length] = "Accept-Encoding: gzip, deflate";
cmdMultiLine[cmdMultiLine.length] = "User-Agent: Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.0; MyIE2)";
cmdMultiLine[cmdMultiLine.length] = "Host: www.blogchina.com";
cmdMultiLine[cmdMultiLine.length] = "Connection: Keep-Alive";
cmdMultiLine[cmdMultiLine.length] = "Cache-Control: no-cache";
cmdMultiLine[cmdMultiLine.length] = "Cookie:
ASPSESSIONIDCSDBRDBB=BEOEIOCAGCMBKIHMDFOKIGLI\r\n\r\n";

var cmd =
Encoding.GetEncoding("ascii").GetBytes(cmdMultiLine.join("\r\n").ToCharArray
());
netStream.Write(cmd,0,cmd.Length);

var read = new Char[256];
var count = readStream.Read( read, 0, 256 );
while (count > 0){
var str = new System.String(read, 0, count);
outputStr[outputStr.length] = str;
count = readStream.Read(read, 0, 256);
}

netStream.Close();
tcpClient.Close();
}

%>

<% = outputStr.join("\r\n") %>
<!--<%=(new Date()-t)%>-->

////////////////////////////////////////////////////////////////////////////
//

thanks in advance
 
M

myhyli

it's so hard to connect to this newsgroup server
wait ,I will release the code in next reply
 
M

myhyli

ok,the code as following:
////////////////////////////////////////////////////////////////////
<%@ page language="JScript" Debug="true"%>
<%@ import namespace="System.IO" %>
<%@ import namespace="System.Net" %>
<%
var src="http://www.blogchina.com/xml/1_rss2.xml";
var outputStr = "";
try{
var myRequest = WebRequest.Create(src);
myRequest.Timeout=60000;
var myResponse = myRequest.GetResponse();
var myResponseStream = myResponse.GetResponseStream();
var encode = System.Text.Encoding.GetEncoding("gb2312");
var readStream = new StreamReader( myResponseStream, encode );
var read = new Char[256];

var count = readStream.Read( read, 0, 256 );
while (count > 0){
var str = new System.String(read, 0, count);
outputStr += str;
count = readStream.Read(read, 0, 256);
}

myResponse.Close();
}catch(e){
outputStr = e.Message;
}
%>
<% =outputStr %>
////////////////////////////////////////////////////////////////////
 
S

Scott Allen

Hi myhyli:

If it is XML you are reading, you might try using the StreamReader
object's ReadToEnd method.

outputStr = reader.ReadToEnd();

If you are still getting an exception, what is the exact message?

--
Scott
http://www.OdeToCode.com/

ok,the code as following:
////////////////////////////////////////////////////////////////////
<%@ page language="JScript" Debug="true"%>
<%@ import namespace="System.IO" %>
<%@ import namespace="System.Net" %>
<%
var src="http://www.blogchina.com/xml/1_rss2.xml";
var outputStr = "";
try{
var myRequest = WebRequest.Create(src);
myRequest.Timeout=60000;
var myResponse = myRequest.GetResponse();
var myResponseStream = myResponse.GetResponseStream();
var encode = System.Text.Encoding.GetEncoding("gb2312");
var readStream = new StreamReader( myResponseStream, encode );
var read = new Char[256];

var count = readStream.Read( read, 0, 256 );
while (count > 0){
var str = new System.String(read, 0, count);
outputStr += str;
count = readStream.Read(read, 0, 256);
}

myResponse.Close();
}catch(e){
outputStr = e.Message;
}
%>
<% =outputStr %>
////////////////////////////////////////////////////////////////////


Scott Allen said:
Hi myhyli:

If you can show us the code you have for using the WebRequest, I'm
sure we can help out with that class. Should be possible and easier
than using sockets.
 
M

myhyli

I think Read() or ReadToEnd() isn't the reason of my problem. In fact, I didn't get any exception,but IE was lost response...it show the progress all the while,it seemed never finish
so I guess,maybe the problem caused by the remote server?
 
M

myhyli

OMG, it gets right now??? I didn't modify the code,but now it can get the url @_@ so strange...
maybe the problem was caused by the remote server in faith

I still want to know how to get response from http server by sockets in asp.net,or what's wrong with my code in the first article
 
S

Scott Allen

Hi myhyli:

I'm glad you have it working.
Debugging application layer protocols usually requires a packet
capture tool like Netmon. I'm sure you can search for one and use it
to compare your socket activity against the spec.
 
M

myhyli

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top