problem with http request

Discussion in 'ASP .Net' started by myhyli, Oct 11, 2004.

  1. myhyli

    myhyli Guest

    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
     
    myhyli, Oct 11, 2004
    #1
    1. Advertising

  2. myhyli

    Scott Allen Guest

    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" <>
    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(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
    >
    >
    >
     
    Scott Allen, Oct 11, 2004
    #2
    1. Advertising

  3. myhyli

    myhyli Guest

    Re:Re: problem with http request

    it's so hard to connect to this newsgroup server
    wait ,I will release the code in next reply
     
    myhyli, Oct 12, 2004
    #3
  4. myhyli

    myhyli Guest

    Re:Re: problem with http request

    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/
    >
     
    myhyli, Oct 12, 2004
    #4
  5. myhyli

    Scott Allen Guest

    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, (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/
    >>
     
    Scott Allen, Oct 12, 2004
    #5
  6. myhyli

    myhyli Guest

    Re:Re: problem with http request

    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/
    >
     
    myhyli, Oct 12, 2004
    #6
  7. myhyli

    Scott Allen Guest

    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, (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/
    >>
     
    Scott Allen, Oct 12, 2004
    #7
  8. myhyli

    myhyli Guest

    Re:Re: problem with http request

    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/
    >
     
    myhyli, Oct 13, 2004
    #8
  9. myhyli

    myhyli Guest

    Re:Re: problem with http request

    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, (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/
    > >>
     
    myhyli, Oct 13, 2004
    #9
  10. myhyli

    Scott Allen Guest

    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, (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
    >
    >
     
    Scott Allen, Oct 13, 2004
    #10
  11. myhyli

    myhyli Guest

    Re:Re: problem with http request

    thanks ^_^
    but...the server isn't belong to me,and I havn't any right to execute program on it......
    you can see the return info at this url:
    http://myhyli.digichina.net/wap/test2.aspx

    another strange thing:

    when I access to the remote http server to get the file:"www.blogchina.com/xml/1_rss2.xml" by telnet on local pc,I can see the remote server(www.blogchina.com) is IIS,

    but with http://myhyli.digichina.net/wap/test2.aspx ,we get a different result: Apache/1.3.31 , why?


    myhyli

    Scott Allen <bitmask@[nospam].fred.net> ÔÚÐÂÎÅ×é microsoft.public.dotnet.framework.aspnet дµÀ£º
    > 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/
    >
     
    myhyli, Oct 14, 2004
    #11
  12. myhyli

    Scott Allen Guest

    Interesting. Maybe they are running ASP.NET on mono and Linux.

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

    On Thu, 14 Oct 2004 09:00:44 +0800, (myhyli) wrote:

    >thanks ^_^
    >but...the server isn't belong to me,and I havn't any right to execute program on it......
    >you can see the return info at this url:
    >http://myhyli.digichina.net/wap/test2.aspx
    >
    >another strange thing:
    >
    >when I access to the remote http server to get the file:"www.blogchina.com/xml/1_rss2.xml" by telnet on local pc,I can see the remote server(www.blogchina.com) is IIS,
    >
    >but with http://myhyli.digichina.net/wap/test2.aspx ,we get a different result: Apache/1.3.31 , why?
    >
    >
    >myhyli
    >
    >Scott Allen <bitmask@[nospam].fred.net> ÔÚÐÂÎÅ×é microsoft.public.dotnet.framework.aspnet дµÀ£º
    >> 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/
    >>
     
    Scott Allen, Oct 14, 2004
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Birtle
    Replies:
    2
    Views:
    2,073
    John Saunders
    Oct 16, 2003
  2. Replies:
    6
    Views:
    5,273
    Tor Iver Wilhelmsen
    Aug 29, 2005
  3. James
    Replies:
    3
    Views:
    16,408
    Roedy Green
    Nov 25, 2005
  4. nRk
    Replies:
    1
    Views:
    1,010
    Steven D'Aprano
    Feb 12, 2009
  5. Gelonida N
    Replies:
    0
    Views:
    530
    Gelonida N
    Feb 27, 2012
Loading...

Share This Page