Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.87 KB | None | 0 0
  1. private synchronized String CreateNewProductPOST (String urlString, String encodedString, String title, String content, Double price, String tags) {
  2.  
  3. String data = "product[title]=" + URLEncoder.encode(title) +
  4. "&product[content]=" + URLEncoder.encode(content) +
  5. "&product[price]=" + URLEncoder.encode(price.toString()) +
  6. "&tags=" + tags;
  7. try {
  8. URL url = new URL(urlString);
  9. URLConnection conn;
  10. conn = url.openConnection();
  11. conn.setRequestProperty ("Authorization", "Basic " + encodedString);
  12. conn.setDoOutput(true);
  13. conn.setDoInput(true);
  14. OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  15. wr.write(data);
  16. wr.flush();
  17. // Get the response
  18. BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  19. String line;
  20. while ((line = rd.readLine()) != null) {
  21. // Process line...
  22. }
  23. wr.close();
  24. rd.close();
  25. return rd.toString();
  26. } catch (MalformedURLException e) {
  27.  
  28. e.printStackTrace();
  29. return e.getMessage();
  30. }
  31. catch (IOException e) {
  32.  
  33. e.printStackTrace();
  34. return e.getMessage();
  35. }
  36. }
  37.  
  38. conn.setRequestProperty ("Authorization", "Basic " + encodedString);
  39.  
  40. BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  41.  
  42. URL url = new URL(urlString);
  43. URLConnection conn = url.openConnection();
  44. conn.setDoOutput(true);
  45. conn.setRequestProperty ("Authorization", encodedCredentials);
  46.  
  47. OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
  48.  
  49. writer.write(data);
  50. writer.flush();
  51. String line;
  52. BufferedReader reader = new BufferedReader(new
  53. InputStreamReader(conn.getInputStream()));
  54. while ((line = reader.readLine()) != null) {
  55. System.out.println(line);
  56. }
  57. writer.close();
  58. reader.close();
  59.  
  60. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  61. conn.setRequestMethod("POST");
  62.  
  63. conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded");
  64. conn.setRequestProperty( "Accept", "*/*" );
  65.  
  66. URL url = new URL(httpurl);
  67. HashMap<String, String> params = new HashMap<String, String>();
  68. params.put("client_id", id);
  69. params.put("client_secret", secret);
  70. params.put("grant_type", "authorization_code");
  71. params.put("redirect_uri", redirect);
  72. params.put("code", code); // your INSTAGRAM code received
  73. Set set = params.entrySet();
  74. Iterator i = set.iterator();
  75. StringBuilder postData = new StringBuilder();
  76. for (Map.Entry<String, String> param : params.entrySet()) {
  77. if (postData.length() != 0) {
  78. postData.append('&');
  79. }
  80. postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
  81. postData.append('=');
  82. postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
  83. }
  84. byte[] postDataBytes = postData.toString().getBytes("UTF-8");
  85.  
  86. HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
  87. conn.setRequestMethod("POST");
  88. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  89. conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
  90. conn.setDoOutput(true);
  91. conn.getOutputStream().write(postDataBytes);
  92. BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
  93. StringBuilder builder = new StringBuilder();
  94. for (String line = null; (line = reader.readLine()) != null;) {
  95. builder.append(line).append("n");
  96. }
  97. reader.close();
  98. conn.disconnect();
  99. System.out.println("INSTAGRAM token returned: "+builder.toString());
  100.  
  101. connection.setDoOutput(true); // Triggers POST.
  102.  
  103. java.io.OutputStreamWriter wr = new java.io.OutputStreamWriter(connection.getOutputStream());
  104. wr.write(textToSend);
  105. wr.flush();
  106.  
  107. package com.yubraj.sample.datamanager;
  108.  
  109. import android.content.Context;
  110. import android.os.AsyncTask;
  111. import android.os.Bundle;
  112. import android.text.TextUtils;
  113. import android.util.Log;
  114.  
  115. import com.yubaraj.sample.utilities.GeneralUtilities;
  116.  
  117.  
  118. import java.io.BufferedInputStream;
  119. import java.io.BufferedReader;
  120. import java.io.BufferedWriter;
  121. import java.io.IOException;
  122. import java.io.InputStream;
  123. import java.io.InputStreamReader;
  124. import java.io.OutputStream;
  125. import java.io.OutputStreamWriter;
  126. import java.io.UnsupportedEncodingException;
  127. import java.net.HttpURLConnection;
  128. import java.net.URL;
  129. import java.net.URLEncoder;
  130. import java.util.HashMap;
  131. import java.util.Map;
  132.  
  133. import javax.net.ssl.HttpsURLConnection;
  134.  
  135. /**
  136. * Created by yubraj on 7/30/15.
  137. */
  138. public class ServerRequestHandler {
  139. private static final String TAG = "Server Request";
  140. OnServerRequestComplete listener;
  141.  
  142. public ServerRequestHandler (){
  143.  
  144. }
  145. public void doServerRequest(HashMap<String, String> parameters, String url, int requestType, OnServerRequestComplete listener){
  146.  
  147. debug("ServerRequest", "server request called, url = " + url);
  148. if(listener != null){
  149. this.listener = listener;
  150. }
  151. try {
  152. new BackgroundDataSync(getPostDataString(parameters), url, requestType).execute();
  153. debug(TAG , " asnyc task called");
  154. } catch (Exception e) {
  155. e.printStackTrace();
  156. }
  157.  
  158. }
  159. public void doServerRequest(HashMap<String, String> parameters, String url, int requestType){
  160. doServerRequest(parameters, url, requestType, null);
  161. }
  162.  
  163. public interface OnServerRequestComplete{
  164. void onSucess(Bundle bundle);
  165. void onFailed(int status_code, String mesage, String url);
  166. }
  167.  
  168. public void setOnServerRequestCompleteListener(OnServerRequestComplete listener){
  169. this.listener = listener;
  170. }
  171.  
  172. private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
  173. StringBuilder result = new StringBuilder();
  174. boolean first = true;
  175. for(Map.Entry<String, String> entry : params.entrySet()){
  176. if (first)
  177. first = false;
  178. else
  179. result.append("&");
  180.  
  181. result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
  182. result.append("=");
  183. result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
  184. }
  185.  
  186. return result.toString();
  187. }
  188.  
  189. class BackgroundDataSync extends AsyncTask<String, Void , String>{
  190. String params;
  191. String mUrl;
  192. int request_type;
  193.  
  194. public BackgroundDataSync(String params, String url, int request_type){
  195. this.mUrl = url;
  196. this.params = params;
  197. this.request_type = request_type;
  198. }
  199.  
  200. @Override
  201. protected void onPreExecute() {
  202. super.onPreExecute();
  203. }
  204.  
  205. @Override
  206. protected String doInBackground(String... urls) {
  207. debug(TAG, "in Background, urls = " + urls.length);
  208. HttpURLConnection connection;
  209. debug(TAG, "in Background, url = " + mUrl);
  210. String response = "";
  211. switch (request_type) {
  212. case 1:
  213. try {
  214. connection = iniitializeHTTPConnection(mUrl, "POST");
  215. OutputStream os = connection.getOutputStream();
  216. BufferedWriter writer = new BufferedWriter(
  217. new OutputStreamWriter(os, "UTF-8"));
  218. writer.write(params);
  219. writer.flush();
  220. writer.close();
  221. os.close();
  222. int responseCode = connection.getResponseCode();
  223. if (responseCode == HttpsURLConnection.HTTP_OK) {
  224. /* String line;
  225. BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream()));
  226. while ((line=br.readLine()) != null) {
  227. response+=line;
  228. }*/
  229. response = getDataFromInputStream(new InputStreamReader(connection.getInputStream()));
  230. } else {
  231. response = "";
  232. }
  233. } catch (IOException e) {
  234. e.printStackTrace();
  235. }
  236. break;
  237. case 0:
  238. connection = iniitializeHTTPConnection(mUrl, "GET");
  239.  
  240. try {
  241. if (connection.getResponseCode() == connection.HTTP_OK) {
  242. response = getDataFromInputStream(new InputStreamReader(connection.getInputStream()));
  243. }
  244. } catch (Exception e) {
  245. e.printStackTrace();
  246. response = "";
  247. }
  248. break;
  249. }
  250. return response;
  251.  
  252.  
  253. }
  254.  
  255. @Override
  256. protected void onPostExecute(String s) {
  257. super.onPostExecute(s);
  258. if(TextUtils.isEmpty(s) || s.length() == 0){
  259. listener.onFailed(DbConstants.NOT_FOUND, "Data not found", mUrl);
  260. }
  261. else{
  262. Bundle bundle = new Bundle();
  263. bundle.putInt(DbConstants.STATUS_CODE, DbConstants.HTTP_OK);
  264. bundle.putString(DbConstants.RESPONSE, s);
  265. bundle.putString(DbConstants.URL, mUrl);
  266. listener.onSucess(bundle);
  267. }
  268. //System.out.println("Data Obtained = " + s);
  269. }
  270.  
  271. private HttpURLConnection iniitializeHTTPConnection(String url, String requestType) {
  272. try {
  273. debug("ServerRequest", "url = " + url + "requestType = " + requestType);
  274. URL link = new URL(url);
  275. HttpURLConnection conn = (HttpURLConnection) link.openConnection();
  276. conn.setRequestMethod(requestType);
  277. conn.setDoInput(true);
  278. conn.setDoOutput(true);
  279. return conn;
  280. }
  281. catch(Exception e){
  282. e.printStackTrace();
  283. }
  284. return null;
  285. }
  286.  
  287. }
  288. private String getDataFromInputStream(InputStreamReader reader){
  289. String line;
  290. String response = "";
  291. try {
  292.  
  293. BufferedReader br = new BufferedReader(reader);
  294. while ((line = br.readLine()) != null) {
  295. response += line;
  296.  
  297. debug("ServerRequest", "response length = " + response.length());
  298. }
  299. }
  300. catch (Exception e){
  301. e.printStackTrace();
  302. }
  303. return response;
  304. }
  305.  
  306. private void debug(String tag, String string) {
  307. Log.d(tag, string);
  308. }
  309. }
  310.  
  311. HashMap<String, String>params = new HashMap<String, String>();
  312. params.put("action", "request_sample");
  313. params.put("name", uname);
  314. params.put("message", umsg);
  315. params.put("email", getEmailofUser());
  316. params.put("type", "bio");
  317. dq.doServerRequest(params, "your_url", DbConstants.METHOD_POST);
  318. dq.setOnServerRequestCompleteListener(new ServerRequestHandler.OnServerRequestComplete() {
  319. @Override
  320. public void onSucess(Bundle bundle) {
  321. debug("data", bundle.getString(DbConstants.RESPONSE));
  322. }
  323.  
  324. @Override
  325. public void onFailed(int status_code, String mesage, String url) {
  326. debug("sample", mesage);
  327.  
  328. }
  329. });
  330.  
  331. URL server = null;
  332. URLConnection conexion = null;
  333. BufferedReader reader = null;
  334. server = new URL("http://localhost:8089/myApp/resources/webService");
  335. conexion = server.openConnection();
  336. reader = new BufferedReader(new InputStreamReader(server.openStream()));
  337. System.out.println(reader.readLine());
  338.  
  339. // HTTP POST request
  340. private void sendPost() throws Exception {
  341.  
  342. String url = "https://selfsolve.apple.com/wcResults.do";
  343. URL obj = new URL(url);
  344. HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
  345.  
  346. //add reuqest header
  347. con.setRequestMethod("POST");
  348. con.setRequestProperty("User-Agent", USER_AGENT);
  349. con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  350.  
  351. String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
  352.  
  353. // Send post request
  354. con.setDoOutput(true);
  355. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  356. wr.writeBytes(urlParameters);
  357. wr.flush();
  358. wr.close();
  359.  
  360. int responseCode = con.getResponseCode();
  361. System.out.println("nSending 'POST' request to URL : " + url);
  362. System.out.println("Post parameters : " + urlParameters);
  363. System.out.println("Response Code : " + responseCode);
  364.  
  365. BufferedReader in = new BufferedReader(
  366. new InputStreamReader(con.getInputStream()));
  367. String inputLine;
  368. StringBuffer response = new StringBuffer();
  369.  
  370. while ((inputLine = in.readLine()) != null) {
  371. response.append(inputLine);
  372. }
  373. in.close();
  374.  
  375. //print result
  376. System.out.println(response.toString());
  377.  
  378. }
  379.  
  380. static String encode(String s, String enc)
  381. Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme.
  382.  
  383. String data = "product[title]=" + title +
  384. "&product[content]=" + content +
  385. "&product[price]=" + price.toString() +
  386. "&tags=" + tags;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement