Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. public String CallSocket(String host,Integer port,String request,String charset) {
  2. StringBuffer result = new StringBuffer();
  3. try {
  4. Socket client = new Socket(host, port);
  5. client.setSoTimeout(10000);
  6. PrintStream out = new PrintStream(client.getOutputStream());
  7. BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream(), charset));
  8. out.println(request);
  9. try {
  10. while (true) {
  11. String echo = in.readLine();
  12. //</Response>...
  13. if (echo.indexOf("</Response>") > -1) {
  14. echo = "</Response>";
  15. result.append("\n" + echo);
  16. break;
  17. }
  18. //...<?xml
  19. Integer start = echo.indexOf("<?xml");
  20. if (start > -1) {
  21. echo = echo.substring(start);
  22. }
  23. result.append("\n" + echo);
  24. }
  25. } catch (SocketTimeoutException e) {
  26. System.out.println("Time out, No response");
  27. }
  28. if (client != null) {
  29. client.close();
  30. }
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. return result.toString();
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement