Loesung zu HTTPS..

  • Antworten:0
Lucius
  • Forum-Beiträge: 32

22.09.2011, 22:17:30 via Website

Hallo Leute,

Ich habe mal ein wenig erforscht und folgenden code gefunden, der die Loesung sein sollte fuer einen HTTPS post.
Ich weiss aber leider nicht wie Ich meine Daten uebergeben sollte, sprich meine URL: https:www//gesichert..und den User und das Pasword.
koennte hier mal jemand nach gucken ob dies wirklich funktioniert?

Danke im voraus.

1import java.io.IOException;
2import java.io.InputStream;
3import java.io.UnsupportedEncodingException;
4import java.net.HttpURLConnection;
5import java.net.URL;
6import java.net.URLConnection;
7
8import org.apache.http.HttpResponse;
9import org.apache.http.client.methods.HttpGet;
10import org.apache.http.client.methods.HttpPost;
11import org.apache.http.client.params.ClientPNames;
12import org.apache.http.client.params.CookiePolicy;
13import org.apache.http.entity.StringEntity;
14import org.apache.http.impl.client.DefaultHttpClient;
15import org.apache.http.params.BasicHttpParams;
16import org.apache.http.params.HttpConnectionParams;
17import org.apache.http.params.HttpParams;
18import org.apache.http.protocol.BasicHttpContext;
19import org.apache.http.protocol.HttpContext;
20import org.apache.http.util.EntityUtils;
21import org.json.JSONObject;
22
23import android.util.Log;
24
25public class HttpRequest{
26
27 DefaultHttpClient httpClient;
28 HttpContext localContext;
29 private String ret;
30
31 HttpResponse response = null;
32 HttpPost httpPost = null;
33 HttpGet httpGet = null;
34
35 public HttpRequest(){
36 HttpParams myParams = new BasicHttpParams();
37
38 HttpConnectionParams.setConnectionTimeout(myParams, 10000);
39 HttpConnectionParams.setSoTimeout(myParams, 10000);
40 httpClient = new DefaultHttpClient(myParams);
41 localContext = new BasicHttpContext();
42 }
43
44 public void clearCookies() {
45 httpClient.getCookieStore().clear();
46 }
47
48 public void abort() {
49 try {
50 if (httpClient != null) {
51 System.out.println("Abort.");
52 httpPost.abort();
53 }
54 } catch (Exception e) {
55 System.out.println("Your App Name Here" + e);
56 }
57 }
58
59 public String sendPost(String url, String data) {
60 return sendPost(url, data, null);
61 }
62
63 public String sendJSONPost(String url, JSONObject data) {
64 return sendPost(url, data.toString(), "application/json");
65 }
66
67 public String sendPost(String url, String data, String contentType) {
68 ret = null;
69
70 httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
71
72 httpPost = new HttpPost(url);
73 response = null;
74
75 StringEntity tmp = null;
76
77 Log.d("Your App Name Here", "Setting httpPost headers");
78
79 httpPost.setHeader("User-Agent", "SET YOUR USER AGENT STRING HERE");
80 httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
81
82 if (contentType != null) {
83 httpPost.setHeader("Content-Type", contentType);
84 } else {
85 httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
86 }
87
88 try {
89 tmp = new StringEntity(data,"UTF-8");
90 } catch (UnsupportedEncodingException e) {
91 Log.e("Your App Name Here", "HttpUtils : UnsupportedEncodingException : "+e);
92 }
93
94 httpPost.setEntity(tmp);
95
96 Log.d("Your App Name Here", url + "?" + data);
97
98 try {
99 response = httpClient.execute(httpPost,localContext);
100
101 if (response != null) {
102 ret = EntityUtils.toString(response.getEntity());
103 }
104 } catch (Exception e) {
105 Log.e("Your App Name Here", "HttpUtils: " + e);
106 }
107
108 Log.d("Your App Name Here", "Returning value:" + ret);
109
110 return ret;
111 }
112
113 public String sendGet(String url) {
114 httpGet = new HttpGet(url);
115
116 try {
117 response = httpClient.execute(httpGet);
118 } catch (Exception e) {
119 Log.e("Your App Name Here", e.getMessage());
120 }
121
122 //int status = response.getStatusLine().getStatusCode();
123
124 // we assume that the response body contains the error message
125 try {
126 ret = EntityUtils.toString(response.getEntity());
127 } catch (IOException e) {
128 Log.e("Your App Name Here", e.getMessage());
129 }
130
131 return ret;
132 }
133
134 public InputStream getHttpStream(String urlString) throws IOException {
135 InputStream in = null;
136 int response = -1;
137
138 URL url = new URL(urlString);
139 URLConnection conn = url.openConnection();
140
141 if (!(conn instanceof HttpURLConnection))
142 throw new IOException("Not an HTTP connection");
143
144 try{
145 HttpURLConnection httpConn = (HttpURLConnection) conn;
146 httpConn.setAllowUserInteraction(false);
147 httpConn.setInstanceFollowRedirects(true);
148 httpConn.setRequestMethod("GET");
149 httpConn.connect();
150
151 response = httpConn.getResponseCode();
152
153 if (response == HttpURLConnection.HTTP_OK) {
154 in = httpConn.getInputStream();
155 }
156 } catch (Exception e) {
157 throw new IOException("Error connecting");
158 } // end try-catch
159
160 return in;
161 }
162}

Antworten