1

i must send one text string using java to a IP web cam, before it take picture. So after I read the camera user manual and searched in google, the only thing i found was using cURL. I install it and its run fine, and everything is okay, the text from the file appear in the video streaming. The command is this curl -T test.xml http://admin:[email protected]/Video/inputs/channels/2/overlays/text/2 and the content of test.xml is:

<TextOverlay xmlns="http://www.hikvision.com/ver10/XMLSchema" version="1.0"> 
<id>2</id>
<enabled>true</enabled>
<posX>5</posX>
<posY>5</posY>
<message>Text here </message>
</TextOverlay>

So I want to send this content using Java, I already tried using post and java.net but I get an error "Server returned HTTP response code: 403 for URL"

Here is my code:

 System.out.println("Starting......");
URL url = new URL("http://192.168.0.1/Video/inputs/channels/2/overlays/text/2/");
String data = "<TextOverlay xmlns=\"http://www.hikvision.com/ver10/XMLSchema\" version=\"1.0\">\n"
    + "<id>2</id>\n"
    + "<enabled>true</enabled>\n"
    + "<posX>5</posX>\n"
    + "<posY>5</posY>\n"
    + "<message>Text here</message>\n"
    + "</TextOverlay>";
HttpURLConnection httpConnection = prepareConn(url, null, "admin", "pass");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty ( "Content-Type", "text/xml" );
OutputStreamWriter out = new OutputStreamWriter(httpConnection.getOutputStream());
out.write(data);
out.flush();
out.close();
System.out.println("Printing......");
System.out.println(httpConnection.getResponseCode());
System.out.println(httpConnection.getResponseMessage());
InputStreamReader reader = new InputStreamReader(httpConnection.getInputStream());
StringBuilder buf = new StringBuilder();
char[] cbuf = new char[2048];
int num;

while(-1 != (num = reader.read(cbuf)))
{
  buf.append(cbuf, 0, num);
}

String result = buf.toString();
System.out.println("\nResponse received from server after POST" + result);  
}


static  private HttpURLConnection prepareConn(final URL url, Properties request_props, String username, String password) throws Error, IOException
{
System.out.println("Authorization......");
if (!url.getProtocol().equalsIgnoreCase("http"))
  throw new Error(url.toString() + " is not HTTP!");

final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(300);
conn.setRequestMethod("POST");

final Properties DEFAULT_REQUEST_PROPS = new Properties();
DEFAULT_REQUEST_PROPS.setProperty("charset", "utf-8");

final Properties props = new Properties(DEFAULT_REQUEST_PROPS);
if (request_props != null)
  for (final String name : request_props.stringPropertyNames())
    props.setProperty(name, request_props.getProperty(name));

for (final String name : props.stringPropertyNames())
  conn.setRequestProperty(name, props.getProperty(name));
if(null != username && null != password)
  conn.setRequestProperty("Authorization", "Basic " + new BASE64Encoder().encode((username+":"+password).getBytes()));
return conn;
}

Hope someone can help :) All the best !

1 Answer 1

2

I just use wrong RequestMethod, after deep research I found that i must use PUT not POST request. Now just change setRequestMethod("POST") to setRequestMethod("PUT") and works like a charm.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks -- I'm doing the exact same thing as you and this came in very useful! Cheers from another Hikvision user!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.