int connType,
String requestMethod,
TweetPhoto tweetPhoto,
boolean uploadData)
{
HttpConnection conn = null;
OutputStream out = null;
if (StringUtilities.startsWithIgnoreCase(url, "www.")) {
url = "http://" + url;
}
String login = tweetPhoto.m_tokenIdentifier+":"+tweetPhoto.m_tokenSecret;
try {
byte[] encoded = Base64OutputStream.encode(login.getBytes(), 0, login.length(), false, false);
if (url.indexOf(";deviceside=") == -1) {
switch (connType) {
case CONNECTION_BES:
url = url + ";deviceside=false";
break;
case CONNECTION_BIS:
url = url + ";XXXXXXXXXXXXXXXX";
break;
case CONNECTION_TCPIP:
url = url + ";deviceside=true";
break;
case CONNECTION_WIFI:
url = url + ";interface=wifi";
}
}
conn = (HttpConnection) Connector.open(url);
if (requestHeaders != null) {
String referer = requestHeaders.getPropertyValue("referer");
boolean sendReferrer = true;
if (referer != null &&
StringUtilities.startsWithIgnoreCase(referer, "https:") &&
!StringUtilities.startsWithIgnoreCase(url, "https:"))
{
sendReferrer = false;
}
int size = requestHeaders.size();
for (int i = 0; i < size;) {
String header = requestHeaders.getPropertyKey(i);
if (!sendReferrer && header.equals("referer")) {
requestHeaders.removeProperty(i);
--size;
continue;
}
String value = requestHeaders.getPropertyValue(i++);
if (value != null) {
conn.setRequestProperty(header, value);
}
}
}
conn.setRequestProperty("Authorization", "Basic " + new String(encoded));
conn.setRequestProperty("TPSERVICE", tweetPhoto.m_ServiceName);
conn.setRequestProperty("TPISOAUTH", tweetPhoto.m_isOAuth ? "True" : "False");
conn.setRequestProperty("TPAPIKEY", tweetPhoto.m_APIKey);
conn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
conn.setRequestMethod(requestMethod);
System.out.println("URL: " + url);
System.out.println("Request Method: " + requestMethod);
System.out.println("Service Name: " + tweetPhoto.m_ServiceName);
System.out.println("is OAuth?: " + (tweetPhoto.m_isOAuth ? "True" : "False"));
System.out.println("TweetPhoto API Key: " + tweetPhoto.m_APIKey);
System.out.println("Authorization: " + "Basic " + new String(encoded));
if (requestMethod.equalsIgnoreCase("PUT") || requestMethod.equalsIgnoreCase("POST")) {
if (uploadData) {
conn.setRequestProperty("TPPOST", "True");
conn.setRequestProperty("TPMIMETYPE", tweetPhoto.m_mimeType);
conn.setRequestProperty("TPUTF8", "True");
conn.setRequestProperty("TPMSG", Base64Coder.encodeString(tweetPhoto.m_comment));
if (tweetPhoto.m_latitude!=0.0 && tweetPhoto.m_longitude!=0.0) {
conn.setRequestProperty("TPLAT", "" +tweetPhoto.m_latitude);
conn.setRequestProperty("TPLONG", "" + tweetPhoto.m_longitude);
}
if (tweetPhoto.m_tags!=null && tweetPhoto.m_tags.length()>0) {
conn.setRequestProperty("TPTAGS", Base64Coder.encodeString(tweetPhoto.m_tags));
}
}
if (postData!=null) {
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(postData.length));
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
out = conn.openOutputStream();
out.write(postData);
out.flush();
}
}
}