import aiohttp
import requests
listen_url = "https://somehttpstreamingurl"
headers = {"User-Agent": "stream client"}
async def async_listen():
with aiohttp.ClientSession() as session:
async with await session.get(url=listen_url, headers=headers) as r:
while True:
message = await r.content.readline()
if message:
print(message)
def sync_listen():
with requests.session() as session:
r = session.send(requests.Request(method="GET", url=listen_url, headers=headers), stream=True)
for message in r.iter_lines():
if message:
print(message)
So these are two examples of how we can get data from a http stream in Python (3.6 is the one I use)
However in Java I can't seem to do the same, and every google search I do I either get server sided streaming related results or file streaming uploads as results. I would like to write Java code equivalent to the Python code above, but http streaming searches are pretty cluttered when searching it for Java, and even worse when adding the "android" keyword to the search query. Does anyone know how to accomplish the Java eqivalent of the Python code above?
If you want to listen for incoming requests, first you need to read the message text and then do any parsing.
The Java Tutorial has a typical example:
There are also numerous questions in StackOverflow dealing with this use case, e.g.: