Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > problem with http request

Reply
Thread Tools

problem with http request

 
 
myhyli
Guest
Posts: n/a
 
      10-11-2004
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(cmdMultiLin e.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




 
Reply With Quote
 
 
 
 
Scott Allen
Guest
Posts: n/a
 
      10-11-2004
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/

On Mon, 11 Oct 2004 17:54:09 +0800, "myhyli" <(E-Mail Removed)>
wrote:

>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(cmdMultiLi ne.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
>
>
>


 
Reply With Quote
 
 
 
 
myhyli
Guest
Posts: n/a
 
      10-12-2004
it's so hard to connect to this newsgroup server
wait ,I will release the code in next reply
 
Reply With Quote
 
myhyli
Guest
Posts: n/a
 
      10-12-2004
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 <bitmask@[nospam].fred.net> 在新闻组 microsoft.public.dotnet.framework.aspnet 写道:
> 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/
>

 
Reply With Quote
 
Scott Allen
Guest
Posts: n/a
 
      10-12-2004
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/

On Tue, 12 Oct 2004 15:34:29 +0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (myhyli) wrote:

>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 <bitmask@[nospam].fred.net> 在新闻组 microsoft.public.dotnet.framework.aspnet 写道:
>> 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/
>>


 
Reply With Quote
 
myhyli
Guest
Posts: n/a
 
      10-12-2004
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?


Scott Allen <bitmask@[nospam].fred.net> 在新闻组 microsoft.public.dotnet.framework.aspnet 写道:
> 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/
>

 
Reply With Quote
 
Scott Allen
Guest
Posts: n/a
 
      10-12-2004
Does the content you are trying to read usually take a long time to
download?

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

On Tue, 12 Oct 2004 22:09:32 +0800, (E-Mail Removed) (myhyli)
wrote:

>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?
>
>
>Scott Allen <bitmask@[nospam].fred.net> 在新闻组 microsoft.public.dotnet.framework.aspnet 写道:
>> 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/
>>


 
Reply With Quote
 
myhyli
Guest
Posts: n/a
 
      10-13-2004
no,only 3,329 Bytes,and when I access the url by IE,it will complete immediately,you can try it:
http://www.blogchina.com/xml/1_rss2.xml

Scott Allen <bitmask@[nospam].fred.net> 在新闻组 microsoft.public.dotnet.framework.aspnet 写道:
> Does the content you are trying to read usually take a long time to
> download?
>
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
>

 
Reply With Quote
 
myhyli
Guest
Posts: n/a
 
      10-13-2004
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


Scott Allen <bitmask@[nospam].fred.net> 在新闻组 microsoft.public.dotnet.framework.aspnet 写道:
> Does the content you are trying to read usually take a long time to
> download?
>
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
>
> On Tue, 12 Oct 2004 22:09:32 +0800, (E-Mail Removed) (myhyli)
> wrote:
>
> >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?
> >
> >
> >Scott Allen <bitmask@[nospam].fred.net> ?úD???×é microsoft.public.dotnet.framework.aspnet D′μà£o
> >> 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/
> >>

 
Reply With Quote
 
Scott Allen
Guest
Posts: n/a
 
      10-13-2004
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.

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

On Wed, 13 Oct 2004 11:52:20 +0800, (E-Mail Removed) (myhyli) wrote:

>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
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abort an HTTP request before the request timed out Gelonida N Python 0 02-27-2012 02:22 AM
urllib2.Request:: http Request sending successfully, but Responsecontains in valid data. nRk Python 1 02-12-2009 12:53 AM
Why getInputStream in a http servlet request isn't getting the datasent by browser HTTP POST action? James Java 3 11-25-2005 11:17 PM
How to enter to .aspx page by http connection using http POST request farazkazmi@gmail.com Java 6 08-29-2005 02:58 PM
Re: Accessing Request.InputStream / Request.BinaryRead *as the request is occuring*: How??? Brian Birtle ASP .Net 2 10-16-2003 02:11 PM



Advertisments