error is thrown "(400) Bad Request"

Discussion in 'ASP.NET / ASP.NET Core' started by albanello, Jun 4, 2018.

  1. ????????
    Can anyone see what is causing error to be throw? Code has been working for at least 8 month!

    I am accessing a RSS feed to display data on my web page. It has been working for months, but suddenly stopped working.
    Available information show below:
    - Rss Feed "http"
    - First part of Rss Feed if I display the source
    - Code showing line at which a error is thrown "(400) Bad Request"
    ????????


    !!!!!!!!
    This Rss feed was working for almost a year!!!!!!!
    !!!!!!!!
    txlottery.org/export/sites/lottery/rss/tlc_latest.xml

    !!!!!!!!
    Starting lines of Rss Feed
    !!!!!!!!
    <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
    - <rss version="2.0">
    - <channel>
    <title>Texas Lottery</title>
    <link>https://www.txlottery.org</link>
    <description>The latest results for the on-line games of the Texas Lottery: Lotto Texas, Mega Millions, Pick 3, Texas Two Step, Cash Five, Daily 4, All or Nothing. Winning numbers are unofficial until verified by the Texas Lottery Commission.</description>
    <language>en-us</language>
    <copyright>Copyright (C) 2018 Texas Lottery</copyright>
    <pubDate>Mon, 28 May 2018 23:00:02 CST</pubDate>
    <lastBuildDate>Mon, 28 May 2018 23:00:02 CST</lastBuildDate>
    - <image>
    https://www.txlottery.org/export/sites/lottery/Images/lottery_logo.gif
    <title>Texas Lottery</title>
    <link>https://www.txlottery.org/</link>
    <description>Texas Lottery</description>
    </image>
    - <item>
    !!!!!!!!!
    All "<item>" not shown
    The item I am using has exactly the same data/syntax I have been reading for months
    !!!!!!!!!

    </item>
    </channel>
    </rss>

    !!!!!!!!
    Code to get Rss feed
    !!!!!!!!
    Try
    'Create/Initialize Web Request
    Dim rssReq As WebRequest = WebRequest.Create(strURL_arg)

    'Create/Initialize Web Proxy
    Dim rssProxy As New WebProxy(strURL_arg, True)

    'Initialize some more Web Request stuff
    rssReq.Proxy = rssProxy
    rssReq.Timeout = 5000

    'Create/Initialize Web Response with Web Request stuff
    Dim rep As WebResponse = rssReq.GetResponse()
    !!!!!!!!
    Try Exception thrown at previous "Dim rep As WebResponse........" code line
    The remote server returned an error: (400) Bad Request.
    !!!!!!!!

    'Create/Initialize XML Text Reader with Web Response stuff
    Dim xtr As New XmlTextReader(rep.GetResponseStream())

    'Fill DataSet with XML Text Reader data
    dstRssFeed.ReadXml(xtr)

    '********
    'CleanUp
    'Done with Xml Text Reader
    xtr.Close()

    'Done with Web Response
    rep.Close()



    ????????
    Can anyone see something in the Rss Feed that would cause my code to stop working?
    ????????

    Thanks
    Frank A
     
    Last edited: Jun 4, 2018
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    What is the value of strURL_arg? Usually 400 Bad Request means you are submitting something incorrectly in the GET/POST request. Has the URL changed?
     
  3. RayH
    Thanks for your response.

    The value of strURL_arg is "http://www.txlottery.org/export/sites/lottery/rss/tlc_latest.xml"
    The URL has not changed. It has been working for months.

    I inadvertently left "http://www." off my original forum thread. If you put the strURL_arg in a browser you will get the Rss feed and if you look at the view source you will see the full Rss feed in xml format.

    But for some reason "Dim rep As WebResponse = rssReq.GetResponse()" throws the "The remote server returned an error: (400) Bad Request." The "rssReq.GetResponse()" works for other Rss feed from other web sites.

    Any help you can give me is appreciated!

    Frank
     
  4. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Frank,

    Take a look at this link. Check to see if your slashes are being escaped.
     
  5. RayH

    Was unable to make any sense of link you gave me (Above my skill level)........ but I found if I just use "ReadXml()" without the "XmlTextReader()" the Rss feed works. The reason I switched to ""XmlTextReader()" originally was so if the Rss feed was not available it would time out gracefully were as "ReadXml()" by itself will just throw a error and exit ungracefully.

    Maybe some day I will understand more
    Frank
     
    RayH likes this.
  6. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Some URLs are encoded (i.e. a slash (/) character will get converted to %2f), so when you are submitting your GET/POST request, it can make a difference. For example:

    http://example.com/embed/ded

    Can get converted to:

    Code:
    http://example.com%2fembed%2fded
    
    So when you are submitting the request, it might have been expecting a / instead of %2f which is why you got the bad request error. Sometimes this makes a difference. That's all I was trying to show you in the above link.
     
    Last edited: Jun 20, 2018

Share This Page