IP Camera viewer in android - ip

I am doing a project in which ,I have to stream the ip camera's live in my app.
For demo I am using DLink camera(DCS 942L) .Please help me how to stream this ip camera's live in my app.

i succeed access to your camera, i make a http query using dlink:dlink as user:password :
1) before all, i encode dlink:dlink using Base64 encoder it give me ZGxpbms6ZGxpbms=
2) i send the query in c/c++ as follow:
sockaddr_in sin;
char *ip="203.125.227.73";
int port=80;
sin.sin_addr.s_addr=inet_addr(ip); // IP of server
sin.sin_family= AF_INET;
sin.sin_port=htons(port);
int sock=socket(AF_INET,SOCK_STREAM,0);
connect(sock, (sockaddr *)&sin, sizeof(sin));
char s[1024];
strcpy(s,"GET /video/mjpg.cgi HTTP/1.1\r\n"
"Authorization: Basic ZGxpbms6ZGxpbms=\r\n\r\n");
send(sock,s,strlen(s),0);
the server send me back this:
HTTP/1.0 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: multipart/x-mixed-replace;boundary=myboundary
--myboundary
Content-Type: image/jpeg
Content-Length: 32616
X-Status: 00000000
X-Tag: 830892
X-Flags: 0
X-Framerate: 20.92
X-Resolution: 640*360
X-Audio: 0
X-Timestamp: 1386117639854
\r\n\r\n followed_by_jpeg_data_stream_that_you_have_to_decode_in_bmp_and_display_it
in my case i use libjpeg in android and turbo-jpeg in windows to decode the stream.
dont forget to change the username and password, you can encode quickly the new user:pass in this web website:
http://www.motobit.com/util/base64-decoder-encoder.asp

Related

Why is my request returning incomplete multipart?

I am using my Arduino to do a multipart/form-data request. I am generating the request completely by myself as follows:
client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.print(F("Content-Type: multipart/form-data; "));
client.print(F("boundary=\"AaB03x\"\r\n"));
client.print(F("Content-Length: "));
client.print(strlen("{json data here}")
+ strlen("{json data here}"));
client.print("\r\nConnection: close\r\n");
// First part
// Boundary
client.print(F("\r\n--AaB03x\r\n"));
// Headers
client.print(F("Content-Disposition: form-data; name=\"header\"\r\n"));
client.print(F("Content-Type: application/ld+json\r\n"));
client.print(F("Content-Length: "));
client.print(strlen("{json data here}"));
client.print(F("\r\n\r\n"));
// Content
client.print(F("{json data here}"));
// Second part
// Boundary
client.print(F("\r\n--AaB03x\r\n"));
// Headers
client.print(F("Content-Disposition: form-data; name=\"payload\"\r\n"));
client.print(F("Content-Type: application/ld+json\r\n"));//
client.print(F("Content-Length: "));
client.print(strlen("{json data here}"));
client.print(F("\r\n\r\n"));
// Content
client.print(F("{json data here}"));
// End of boundary
client.print(F("\r\n--AaB03x--\r\n\r\n"));
However the server returns "Incomplete multipart" which I don't understand since the multipart seems completely fine. I thought it was maybe due to incorrect newlines but I haven't been able to figure out a solution.
The output can be seen below and includes the request sent as well as the response from the server.
GET /router HTTP/1.1
Host: 192.168.178.147
Content-Type: multipart/form-data; boundary="AaB03x"
Content-Length: 4363
Connection: close
--AaB03x
Content-Disposition: form-data; name="header"
Content-Type: application/ld+json
Content-Length: 4143
{
some data here
}
--AaB03x
Content-Disposition: form-data; name="payload"
Content-Type: application/ld+json
Content-Length: 220
{
some data here
}
--AaB03x--
connected to 192.168.178.147
HTTP/1.1 500 Server Error
Connection: close
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=iso-8859-1
Content-Length: 597
Server: Jetty(9.4.41.v20210516)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 500 Server Error</title>
</head>
<body><h2>HTTP ERROR 500 Server Error</h2>
<table>
<tr><th>URI:</th><td>/router</td></tr>
<tr><th>STATUS:</th><td>500</td></tr>
<tr><th>MESSAGE:</th><td>Server Error</td></tr>
<tr><th>SERVLET:</th><td>org.apache.camel.component.jetty.CamelContinuationServlet-38cb1606</td></tr>
<tr><th>CAUSED BY:</th><td>java.io.IOException: Incomplete Multipart</td></tr>
</table>
<hr>Powered by Jetty:// 9.4.41.v20210516<hr/>
</body>
</html>
Your Content-Length calculations are suspect.
Drop the Content-Length headers for each sub-section in the multipart and try again, those are not needed for multipart, and your calculations are just wrong anyway.
You can see captures of various multipart requests from various libraries and browsers in the Jetty tests.
https://github.com/eclipse/jetty.project/tree/jetty-9.4.41.v20210516/jetty-http/src/test/resources/multipart
(Look at the ones ending in *.raw, which you can generally open in a text editor)
Tip: don't do this yourself, multipart mime is full of edge cases, traps, and ancient tricks. Go grab apache httpcomponents httpmime jar and just use it to generate your raw mime multipart section properly.
Artifacts - https://search.maven.org/artifact/org.apache.httpcomponents/httpmime
Javadoc - https://javadoc.io/doc/org.apache.httpcomponents/httpmime/latest/index.html

Arduino EtherCard Post StaticJsonDocument to a web server

I am working on a project which I need to post some sensor data to my web service over tcp. I used StaticJsonDocument to hold these sensor data. The problem is that with the code below I can not be able to post any data. Currently I am using ENC28J60 for the ethernet connection.
void sendToApi (StaticJsonDocument<600> root) {
byte sd = stash.create();
String json_string;
serializeJson(root, json_string);
stash.print(json_string);
stash.save();
int stash_size = stash.size();
Serial.println(stash_size);
Stash::prepare(PSTR("POST /api/module HTTP/1.1" "\r\n"
"Host: 192.168.1.5:8181" "\r\n"
"Content-Type: application/json" "\r\n"
"Content-Length: $D" "\r\n"
"Authorization: Basic bWV0Ok1ldEF0czE4Kio=" "\r\n"
"\r\n"
"$H"),
stash_size, sd);
session = ether.tcpSend();
//Serial.println(session);
delay(2000);
}
There is no problem with the ethernet controller (I can ping Google). Also There is no problem with the server side. The postman HTTP request is below.
POST /api/module/ HTTP/1.1
Host: 192.168.1.5:8181
Content-Type: application/json
Authorization: Basic bWV0Ok1ldEF0czE4Kio=
Cache-Control: no-cache
Postman-Token: e160d927-7e05-414f-e0d8-102f3d039ce3
{"id":"0001","module_no":1,"m1":22.5625,"m2":22.5625,"m3":22.5625,"m4":22.5625,"m5":22.5625,"m6":22.5625,"m7":22.5625,"m8":22.5625,"m9":22.5625,"m10":22.5625,"m11":22.5625,"m12":22.5625,"m13":22.5625,"m14":22.5625,"m15":22.5625,"m16":22.5625,"t1":22.5625,"t2":22.5625,"t3":22.5625,"t4":22.5625,"t5":22.5625,"t6":22.5625,"t7":22.5625,"t8":22.5625,"t9":22.5625,"t10":22.5625,"t11":22.5625,"t12":22.5625,"t13":22.5625,"t14":22.5625,"t15":22.5625,"t16":22.5625,"af":22.5625,"uf":22.5625,"sg":22.5625,"sc":22.5625,"a1":946,"a2":946,"a3":946,"a4":946,"a5":946,"a6":32,"a7":32,"a8":32,"a9":946,"a10":946,"a11":946,"a12":946,"a13":32,"a14":32,"a15":32,"a16":32}
So what am I missing here?

How to parse chunked HTTP content with Lua on nodemcu?

I have script which coomunicates between nodemcu and my server. It works good on my localhost and is parsing response retrieved from my server when I send GET request. Problem is when I upload it all on my website where transfer encoding is chunked. I am not able to retrieve content, although request is legitimate and correct. Code is written in Lua and I am trying to work on my NodeMCU device.
conn=net.createConnection(net.TCP, 0)
conn:on("connection",function(conn, payload)
conn:send("GET /mypath/node.php?id=1&update"..
" HTTP/1.1\r\n"..
"Host: www.mydomain.com\r\n"..
"Accept: */*\r\n"..
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
"\r\n\r\n")
end)
conn:on("receive", function(conn, payload)
if string.find(payload, "UPDATE")~=nil then
node.restart()
end
conn:close()
conn = nil
end)
conn:connect(80,"www.mydomain.com")
end
Just to repeat that this GET request works and is tested manualy and on localhost. Only problem is with chunked content, I don't know how to parse it.
Update: I managed to remove chunked encoding by changing HTTP/1.1 to HTTP/1.0, but still I have problem
using this code
conn:on("receive", function(conn, payload)
print(payload)
I get this response
HTTP/1.1 200 OK
Date: Tue, 09 Jan 2018 02:34:25 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=9m226vr20r4baa634bagk8k2k3; path=/
Connection: close
Content-Type: text/html; charset=utf-8
Update 2.
I have just created one file http.php with text included "php". I have uploaded it to localhost and to my domain. Once I tried to access my localhost from nodemcu, and then to domain. Results were different
This is the request
conn:send("GET /"..s.path.."/http.php"..
" HTTP/1.0\r\n"..
"Host: "..s.domain.."\r\n"..
"Accept: */*\r\n"..
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
"\r\n\r\n")
end)
s.domain and s.path correcponds to different paths and domains on localhost and my domain
Result on domain
HTTP/1.1 200 OK
Date: Tue, 09 Jan 2018 03:09:28 GMT
Server: Apache
Connection: close
Content-Type: text/html; charset=UTF-8
result on localhost
TTP/1.1 200 OK
Date: Tue, 09 Jan 2018 03:08:48 GMT
Server: Apache/2.4.27 (Win64) PHP/7.0.23
X-Powered-By: PHP/7.0.23
Content-Length: 3
Connection: close
Content-Type: text/html; charset=UTF-8
php
As you can see, localhost is showing content "php", and domain is showing only header. When I type some file which does not exists domain is showing me html code.
I'm using the following code to put the chunks together. I'm wondering anyways, why your response from the server is missing the Content-Length header.
conn:on("receive", function(client, payload)
-- Inspired by https://github.com/marcoskirsch/nodemcu-httpserver/blob/master/httpserver.lua
-- Collect data packets until the size of HTTP body meets the Content-Length stated in header
if payload:find("Content%-Length:") or bBodyMissing then
if fullPayload then fullPayload = fullPayload .. payload else fullPayload = payload end
if (tonumber(string.match(fullPayload, "%d+", fullPayload:find("Content%-Length:")+16)) > #fullPayload:sub(fullPayload:find("\r\n\r\n", 1, true)+4, #fullPayload)) then
bBodyMissing = true
return
else
payload = fullPayload
fullPayload, bBodyMissing = nil
end
end
if (bBodyMissing == nil) then
local _, headerEnd = payload:find("\r\n\r\n")
local body = payload:sub(headerEnd + 1)
print (body)
end
end)

Qt: Use tcp socket to get google map image?

For some reason I need to use a blocking call to perform image accessing from google's server.
However, QNetworkAccessManager seems to be async, though there are many work arounds, like calling a eventLoop.exec(); many people online suggested me not to do so.
So I am trying to use TCP socekt.
I want to access the image here:
http://mt1.google.com/vt/lyrs=y&x=0&y=0&z=0
And here is my code:
socket = new QTcpSocket(this);
socket->connectToHost("mt1.google.com", 80, QIODevice::ReadWrite);
if(socket->waitForConnected(5000))
{
qDebug() << "Connected!";
// send
socket->write("/vt/lyrs=y&x=0&y=0&z=0");
socket->waitForBytesWritten(1000);
socket->waitForReadyRead(3000);
qDebug() << "Reading: " << socket->bytesAvailable();
// get the data
qDebug() << socket->readAll();
// close the connection
socket->close();
}
else
{
qDebug() << "Not connected!";
}
But it seems to working at all? What should I write through the tcp socket to get the image?
TCP provides only the transport mechanism. Since you are trying to communicate with a web server, you should compose HTTP messages.
Replace the line
socket->write("/vt/lyrs=y&x=0&y=0&z=0");
with
socket->write("GET /vt/lyrs=y&x=0&y=0&z=0 HTTP/1.1\r\nHost: mt1.google.com\r\nUser-Agent: TestAgent\r\n\r\n");
And you should get the following response :
HTTP/1.1 200 OK
Date: Sun, 14 Jun 2015 14:24:40 GMT
Expires: Sun, 14 Jun 2015 14:24:40 GMT
Cache-Control: private, max-age=3600
Access-Control-Allow-Origin: *
Content-Type: image/jpeg
X-Content-Type-Options: nosniff
Server: paintfe
Content-Length: 10790
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic,p=0
IMAGEDATA
Parse the response and extract the IMAGEDATA part.
EDIT : TCP delivers the response divided into chunks. With this approach, you will not be able to receive the whole response since you are trying to receive it in one go.
You should examine the Content-Length header and wait until receiveing the specified amount of bytes.

Asp.Net Sending PDF to browser

I've been trying to get this aspx page to serve up a pdf. It works correctly in Firefox, but IE gives
Internet Explorer cannot download getform.aspx from SERVER_NAME
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found.
This is the general functionality of my code. It's spread across multiple functions (this is why we're not using WriteFile - sometimes we generate the pdf on the fly), but this is generally it:
FileStream fs = File.Open(Path.Combine(PdfBasePath, "form.pdf"), FileMode.Open, FileAccess.Read);
Stream output = Response.OutputStream;
byte[] buffer = new byte[BUFFER_SIZE];
int read_count = fs.Read(buffer, 0, BUFFER_SIZE);
while (read_count > 0)
{
output.Write(buffer, 0, read_count);
read_count = fs.Read(buffer, 0, BUFFER_SIZE);
}
fs.Close();
Response.Clear();
Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Pdf;
Response.AddHeader("Content-Disposition", "attachment; filename=form.pdf");
Response.Output.Flush();
Response.End();
Looking at Fiddler, the page is being fetched using this:
GET /getform.aspx?Failure=Y&r=someencryptedstring HTTP/1.1
It is being returned to the browser thus:
HTTP/1.1 200 OK
Date: Thu, 09 Apr 2009 22:08:33 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Pragma: no-cache
Content-Disposition: attachment; filename=form.pdf
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: -1
Content-Type: application/pdf
Content-Length: 628548
This is really bugging me. I'm not using SSL, otherwise this KB article would seem to apply. Anyone have any ideas?
Is the Content-Length being returned in the header actually correct for the file you're sending? I'm just comparing this to some production code we use here and it looks like we explicitly set the Content-Length header. If I recall correctly, some browsers have a problem if the header and the actual file size don't match.
Edit
The question author found that changing the Content-Disposition header to application/download instead of application/pdf seems to work around the problem.

Resources