List of usage examples for org.apache.http.client.entity UrlEncodedFormEntity writeTo
public void writeTo(OutputStream outputStream) throws IOException
From source file:fr.qinder.api.APIGetter.java
protected HttpsURLConnection post(String sUrl, APIRequest request) { HttpsURLConnection urlConnection; URL url;/*from w w w .j a v a2s . c om*/ try { url = new URL(sUrl); urlConnection = (HttpsURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); if (request.getPosts().size() != 0) { urlConnection.setRequestMethod("POST"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(request.getPosts()); OutputStream post = urlConnection.getOutputStream(); entity.writeTo(post); post.flush(); } urlConnection.connect(); } catch (IOException e) { urlConnection = null; } return urlConnection; }
From source file:in.rab.ordboken.NeClient.java
private void loginMainSite() throws IOException, LoginException { ArrayList<BasicNameValuePair> data = new ArrayList<BasicNameValuePair>(); data.add(new BasicNameValuePair("_save_loginForm", "true")); data.add(new BasicNameValuePair("redir", "/success")); data.add(new BasicNameValuePair("redirFail", "/fail")); data.add(new BasicNameValuePair("userName", mUsername)); data.add(new BasicNameValuePair("passWord", mPassword)); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(data); URL url = new URL("https://www.ne.se/user/login.jsp"); HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); https.setInstanceFollowRedirects(false); https.setFixedLengthStreamingMode((int) entity.getContentLength()); https.setDoOutput(true);/*from w w w . ja v a 2 s . c om*/ try { OutputStream output = https.getOutputStream(); entity.writeTo(output); output.close(); Integer response = https.getResponseCode(); if (response != 302) { throw new LoginException("Unexpected response: " + response); } String location = https.getHeaderField("Location"); if (!location.contains("/success")) { throw new LoginException("Failed to login"); } } finally { https.disconnect(); } }