List of usage examples for java.net HttpURLConnection setDoOutput
public void setDoOutput(boolean dooutput)
From source file:com.canoo.cog.sonar.SonarServiceTest.java
@Test public void testAuthenticationSonar() throws IOException { try {//from w ww .ja v a 2 s .co m URL url = new URL("https://ci.canoo.com/sonar/api/resources"); String encoding = new String(Base64.encodeBase64("christophh:".getBytes())); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setRequestProperty("Authorization", "Basic " + encoding); InputStream content = (InputStream) connection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(content)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } }
From source file:org.openmrs.module.casauth.web.controller.LoginInterceptController.java
@Override public ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse res) throws MalformedURLException, IOException { String casTicket = req.getParameter("casticket"); //CAS ticket is only available after login has completed and needs validation if (casTicket == null) { User autheticatedUser = Context.getAuthenticatedUser(); if (autheticatedUser == null) { String casLoginUrl = Context.getAdministrationService().getGlobalProperty("casauth.endpoint.login"); String casAppCode = Context.getAdministrationService() .getGlobalProperty("casauth.endpoint.appcode"); return new ModelAndView( "redirect:" + casLoginUrl + "?cassvc=" + casAppCode + "&casurl=" + req.getRequestURL()); }//from w ww. j av a2s . c om } else { String casValidateUrl = Context.getAdministrationService() .getGlobalProperty("casauth.endpoint.validate"); String casAppCode = Context.getAdministrationService().getGlobalProperty("casauth.endpoint.appcode"); URL url = new URL(casValidateUrl + "?cassvc=" + casAppCode + "&casticket=" + casTicket + "&casurl=" + req.getRequestURL()); //a HTTP connection is created to validate the casticket HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setDoOutput(true); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); try { /** * The CAS validate response is yes on first line, followed by username on next line */ if (in.readLine().equals("yes")) { String superuserUsername = Context.getAdministrationService() .getGlobalProperty("casauth.superuser.username"); String superuserPassword = Context.getAdministrationService() .getGlobalProperty("casauth.superuser.zpassword"); String user = in.readLine(); Context.authenticate(superuserUsername, superuserPassword); Context.becomeUser(user); } } catch (ContextAuthenticationException casex) { Context.logout(); } in.close(); } return new ModelAndView("redirect:/index.htm"); }
From source file:org.jboss.pnc.buildagent.server.TestGetRunningProcesses.java
private HttpURLConnection retrieveProcessList() throws IOException { URL url = new URL("http://" + HOST + ":" + PORT + "/processes"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true);//from w w w.jav a 2 s .co m return connection; }
From source file:org.wso2.msf4j.client.test.ClientTest.java
protected HttpURLConnection request(String path, String method, boolean keepAlive) throws IOException { URL url = baseURI.resolve(path).toURL(); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); if (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)) { urlConn.setDoOutput(true); }/* ww w . j a v a2s . c om*/ urlConn.setRequestMethod(method); if (!keepAlive) { urlConn.setRequestProperty(HEADER_KEY_CONNECTION, HEADER_VAL_CLOSE); } return urlConn; }
From source file:io.mingle.v1.Connection.java
public Response run(String comprehension) { String expr = "{ \"query\": \"" + comprehension + "\", \"limit\": 10000 }"; try {//from w w w . j a v a 2 s . com HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); conn.connect(); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(expr, 0, expr.length()); out.flush(); out.close(); InputStream in = conn.getInputStream(); ByteArrayOutputStream buf = new ByteArrayOutputStream(); byte[] chunk = new byte[4096]; int read = 0; while ((read = in.read(chunk)) > 0) { buf.write(chunk, 0, read); } in.close(); String str = buf.toString(); System.out.println("GOT JSON: " + str); return new Response(JSONValue.parse(str)); } catch (Exception e) { System.err.printf("failed to execute: %s\n", expr); e.printStackTrace(); } return null; }
From source file:com.tonchidot.nfc_contact_exchanger.lib.PictureUploader.java
private String doFileUploadJson(String url, String fileParamName, byte[] data) { try {/* w w w. j a v a 2s .c om*/ String boundary = "BOUNDARY" + new Date().getTime() + "BOUNDARY"; String lineEnd = "\r\n"; String twoHyphens = "--"; URL connUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) connUrl.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"" + fileParamName + "\";filename=\"photo.jpg\"" + lineEnd); dos.writeBytes(lineEnd); dos.write(data, 0, data.length); dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); dos.flush(); dos.close(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder result = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { result.append(line); } rd.close(); return result.toString(); } catch (IOException e) { if (Config.LOGD) { Log.d(TAG, "IOException : " + e); } } return null; }
From source file:net.gcolin.simplerepo.test.AbstractRepoTest.java
protected int sendContent(String url, String fileContent, String user, String password) throws IOException { HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection(); try {/*from ww w .j av a2 s . co m*/ c.setDoOutput(true); c.setRequestMethod("PUT"); if (user != null) { c.setRequestProperty("Authorization", "Basic " + Base64.encodeBase64String((user + ":" + password).getBytes("utf-8"))); } c.connect(); IOUtils.copy(new ByteArrayInputStream(fileContent.getBytes("utf-8")), c.getOutputStream()); return c.getResponseCode(); } finally { c.disconnect(); } }
From source file:integration.webhose.WebhoseIOClient.java
/** * Convenient method to fetch response from server * //from www. j a v a 2 s . c o m * @param rawUrl Server URL * @return JSONObject Converted server response string * @throws IOException * @throws URISyntaxException */ public JsonElement getResponse(String rawUrl) throws IOException, URISyntaxException { URL url = new URL(rawUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); // Get Response InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); StringBuilder response = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { response.append(line); } rd.close(); JsonParser parser = new JsonParser(); JsonElement o = parser.parse(response.toString()); // Set next query URL mNext = WEBHOSE_BASE_URL + o.getAsJsonObject().get("next").getAsString().replace("\"", ""); return o; }
From source file:fr.ybonnel.simpleweb4j.util.SimpleWebTestUtil.java
public UrlResponse doMethod(String requestMethod, String path, String body) throws Exception { URL url = new URL("http://localhost:" + port + path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(requestMethod); if (body != null) { connection.setDoOutput(true); connection.getOutputStream().write(body.getBytes()); }//from www.java 2 s . c om connection.connect(); UrlResponse response = new UrlResponse(); response.status = connection.getResponseCode(); response.headers = connection.getHeaderFields(); response.contentType = connection.getContentType(); if (response.status >= 400) { if (connection.getErrorStream() != null) { response.body = IOUtils.toString(connection.getErrorStream()); } } else { if (connection.getInputStream() != null) { response.body = IOUtils.toString(connection.getInputStream()); } } return response; }
From source file:com.example.austin.test.TextActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_text); Thread thread = new Thread(new Runnable() { @Override/*from w ww . j a va 2 s .com*/ public void run() { try { Bundle bundle = getIntent().getExtras(); Bitmap photo = (Bitmap) bundle.get("photo"); ByteArrayOutputStream stream1 = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.PNG, 100, stream1); byte[] fileContent = stream1.toByteArray(); String license_code = "59D1D7B4-61FD-49EB-A549-C77E08B7103A"; String user_name = "austincheng16"; String ocrURL = "http://www.ocrwebservice.com/restservices/processDocument?gettext=true"; URL url = new URL(ocrURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Authorization", "Basic " + new String(Base64.encodeBase64((user_name + ":" + license_code).getBytes()))); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Content-Length", Integer.toString(fileContent.length)); try { OutputStream stream = connection.getOutputStream(); stream.write(fileContent); stream.close(); int httpCode = connection.getResponseCode(); if (httpCode == HttpURLConnection.HTTP_OK) { String jsonResponse = GetResponseToString(connection.getInputStream()); PrintOCRResponse(jsonResponse); } else { String jsonResponse = GetResponseToString(connection.getErrorStream()); JSONParser parser = new JSONParser(); JSONObject jsonObj = (JSONObject) parser.parse(jsonResponse); Log.i("austin", "Error Message: " + jsonObj.get("ErrorMessage")); } connection.disconnect(); } catch (IOException e) { Log.i("austin", "IOException"); } } catch (Exception e) { e.printStackTrace(); } } }); thread.start(); }