WebPost.java 4.37 KB
package org.emercit.model;


import java.io.*;
import java.net.*;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Map;

import org.json.simple.*;

import com.google.gson.*;

import org.emercit.config.Config;
import org.emercit.service.SData;

public class WebPost {

	private Config config;
	
	public WebPost(Config config) {
		this.config=config;
	}

	
	

	public  ModelResponse Request() {
		


		  ModelResponse md = null;

			StringBuilder sb = new StringBuilder();

			HttpURLConnection urlConnection = null;
			
			
			BufferedReader br=null;
			
			try {
				
			
					URL url = new URL("http://"+ config.getService()+":8088/api/DataSzs/");

					Proxy proxy;

					if (config.getProxyEnabled()) {
						proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
								config.getProxyServer(),
								config.getProxyPort()));
						
					//	System.out.println(config.getProxyServer()+":"+config.getProxyPort());
						urlConnection = (HttpURLConnection) url.openConnection(proxy);
					} else {
						urlConnection = (HttpURLConnection) url.openConnection();
					}

				urlConnection.setDoOutput(true);
				urlConnection.setRequestMethod("POST");
				urlConnection
						.setRequestProperty("Content-Type", "application/json");
				
				urlConnection.setConnectTimeout(5000);
				urlConnection.setReadTimeout(10000);
	

				JSONObject jsonParam = new JSONObject();

				jsonParam.put("RegKey", config.getRegKey());
				jsonParam.put("SoftVersion", config.getVersion());
				jsonParam.put("IDTeamViewer","");

				OutputStreamWriter out = new OutputStreamWriter(
						urlConnection.getOutputStream());
				out.write(jsonParam.toString());
				out.close();

	
				
				int HttpResult = urlConnection.getResponseCode();

				if (HttpResult == HttpURLConnection.HTTP_OK) {

			    br = new BufferedReader(new InputStreamReader(
							urlConnection.getInputStream(), "utf-8"));

					String line = null;

					while ((line = br.readLine()) != null) {
						sb.append(line + "\n");
					}

			
					br.close();

					Gson gson = new Gson();

					md = gson.fromJson(sb.toString(), ModelResponse.class);
			
				
				}
				br.close();
				urlConnection.disconnect();
		
				SData.INSTANCE.setIsConnect(true);
				
			} catch (Exception e) {
			
				SData.INSTANCE.setIsConnect(false);
	
			} finally {
				
			
				try {
					if (urlConnection != null) {
						urlConnection.disconnect();
					}
					if (br!=null) {
						br.close();
					}
						
				} catch (Exception ex) {
					
					SData.INSTANCE.setIsConnect(false);
		
				}
			}
			
		
		
			return md;
		
	}
	
	
	public void Confirm(MdlEvent event) { 
		
	
			HttpURLConnection urlConnection = null;
			try {
				
				
				
				URL url = new URL("http://"+config.getService()+":8088/api/ConfirmLevelsRivers/");

				Proxy proxy;

				if (config.getProxyEnabled()) {
					proxy = new Proxy(Proxy.Type.HTTP,
							new InetSocketAddress(config.getProxyServer(),
									config.getProxyPort()));
					urlConnection = (HttpURLConnection) url
							.openConnection(proxy);
				} else {
					urlConnection = (HttpURLConnection) url
							.openConnection();
				}

				urlConnection.setDoOutput(true);
				urlConnection.setRequestMethod("POST");
				urlConnection.setRequestProperty("Content-Type",
						"application/json");


				urlConnection.setConnectTimeout(5000);
				urlConnection.setReadTimeout(10000);
	
				
				// Create JSONObject here
				JSONObject jsonParam = new JSONObject();

				jsonParam.put("RegKey",
						config.getRegKey());
				jsonParam.put("id_agk", event.getIdAgk());
				jsonParam.put("id_webapi_message", event.getId());

				OutputStreamWriter out = new OutputStreamWriter(
						urlConnection.getOutputStream());
				out.write(jsonParam.toString());
				out.close();

				int HttpResult = urlConnection.getResponseCode();

				if (HttpResult == HttpURLConnection.HTTP_OK) {

				}
		
				
				urlConnection.disconnect();
			} catch (Exception e) {
	
				
			} finally {
				try {
					if (urlConnection != null) {
						urlConnection.disconnect();
					}

				} catch (Exception ex) {
				
					
					
				}
			}
		
		
	}
	
	
}