📄 httptransportagent.java
字号:
//#endif
Log.info("[sendMessage] requestURL: [" + requestURL +"]");
c = (HttpConnection)Connector.open(requestURL,
Connector.READ_WRITE, true);
//Request Configuration: Added ACCEPT ENCODING Parameter
setConfig(c, request.length);
writeRequest(c, request);
Log.info("[sendMessage] message sent, waiting for response.");
//Receive response on is Inputstream
is = c.openInputStream();
logHeaders(c);
long len = c.getLength();
Log.info("HttpResponse length: " + len);
// Check http error
int httpCode = c.getResponseCode();
Log.info("[sendMessage] Http Code: " + httpCode);
if (httpCode != c.HTTP_OK) {
throw new SyncException(SyncException.SERVER_ERROR,
"Http error: code=[" + httpCode + "] msg=["
+ c.getResponseMessage() + "]");
}
responseDate = c.getHeaderField(PROP_DATE);
Log.info("[sendMessage] Date from server: " + responseDate);
contentType = c.getHeaderField(PROP_CONTENT_ENCODING);
Log.info("[sendMessage] Encoding Response Type from server: " + contentType);
if ((len == -1) && (responseDate == null) && (contentType == null)) {
Log.error("[sendMessage] Error in http response, not reading stream...");
} else {
data = StreamReaderFactory.getStreamReader(contentType).readStream(is, (int)len);
Log.info("[sendMessage] Stream correctly processed.");
break;
}
} catch (ConnectionNotFoundException e) {
String msg = "HttpTransportAgent: can't open connection -->"
+ e.toString() ;
os=null;
Log.error(msg);
throw new SyncException(SyncException.ACCESS_ERROR, msg);
} catch (IOException e) {
Log.error("[sendMessage] Attempt n." + (i+1) + " failed. Retrying...");
Log.error("[sendMessage] " + e);
e.printStackTrace();
if (i == NUM_RETRY-1) {
String msg = "HttpTransportAgent: connection broken --> "
+ e.toString() ;
Log.error(msg);
throw new SyncException(SyncException.ACCESS_ERROR, msg);
}
} catch (IllegalArgumentException e) {
String msg = "HttpTransportAgent: invalid argument for connection --> "
+ e.toString() ;
Log.error(msg);
throw new SyncException(SyncException.ACCESS_ERROR, msg);
} finally {
if (is != null) {
try {
is.close();
is=null;
} catch(IOException ioe){
ioe.printStackTrace();
Log.error("sendmessage: can't close input stream.");
}
}
if (os != null) {
try {
os.close();
os=null;
} catch(IOException ioe){
ioe.printStackTrace();
Log.error("sendmessage: can't close output stream.");
}
}
if (c != null) {
try {
c.close();
c=null;
} catch(IOException ioe){
ioe.printStackTrace();
Log.error("sendmessage: can't close connection.");
}
}
}
}
return data;
}
private void writeRequest(final HttpConnection c, final byte[] request) throws IOException {
OutputStream os = null;
os = c.openOutputStream();
os.write(request);
os.close();
Log.info("[writeRequest] Request written.");
}
// ---------------------------------------------------------- Private methods
/**
* Add request properties for the configuration, profiles,
* and locale of this system.
* @param c current HttpConnection to receive user agent header
*/
private void setConfig(HttpConnection c, int length) throws IOException {
String ua = this.userAgent;
String locale = System.getProperty(PROP_MICROEDITION_LOCALE);
if (ua == null) {
// Build the default user agent
String conf = System.getProperty(PROP_MICROEDITION_CONFIGURATION);
String prof = System.getProperty(PROP_MICROEDITION_PROFILES);
ua = "Profile/" + prof + " Configuration/" + conf ;
}
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("Connection", "Close");
c.setRequestProperty(PROP_CONTENT_TYPE, "application/vnd.syncml+xml");
c.setRequestProperty(PROP_CONTENT_LENGTH, String.valueOf(length));
if (length == 0) {
Log.error("content length has been set to 0 !");
}
// workaround to avoid errors on server
// the client user agent must not be sent with User-Agent header
c.setRequestProperty(PROP_USER_AGENT, "Funambol Java Mail" );
// we use Device-Agent for such an issue
c.setRequestProperty(PROP_DEVICE_AGENT, ua );
// If Set-Cookie header is set to empty value in http
// server response, in Nokia S60 3ed. FP1 devices the Application crashes
// It's due to a Symbian KVM bug.
// A specific workaround has been implemented: the client sends a specific header
// 'x-funambol-force-cookies' to force the server to set a 'Set-Cookie' header not empty.
if (forceCookies) {
c.setRequestProperty(PROP_FORCE_COOKIES, "true" );
}
//Set Encoding and accepted properties: inflater or Gzip input Stream
if (enableCompression){
c.setRequestProperty(PROP_ACCEPT_ENCODING, COMPRESSION_TYPE_GZIP);
Log.debug("Encoding Response Required from Client: " + COMPRESSION_TYPE_GZIP);
}
if (this.sizeThreshold != 0) {
c.setRequestProperty(PROP_SIZE_THRESHOLD,
String.valueOf(this.sizeThreshold));
}
if (locale != null) {
c.setRequestProperty(PROP_CONTENT_LANGUAGE, locale);
}
}
public void enableCompression(boolean enable) {
this.enableCompression = enable;
}
public void setThreshold(int threshold) {
this.sizeThreshold = threshold;
}
public void setRequestURL(String requestURL) {
this.requestURL = requestURL;
}
/**
* Returns the last response date, if available, or null.
*/
public String getResponseDate() {
return responseDate;
}
private void logHeaders(final HttpConnection c) throws IOException {
// debug section for reading http response headers
if (Log.getLogLevel() >= Log.INFO) {
StringBuffer sbh = new StringBuffer();
String tmp = "";
for (int h = 0; h<12; h++) {
tmp = c.getHeaderFieldKey(h);
sbh.append("[").append(h).append("] - ").
append(tmp);
if ((tmp!=null)&&(!tmp.equals(""))) {
sbh.append(": ").append(c.getHeaderField(tmp)).append("\n");
} else {
sbh.append("\n");
}
}
Log.info("header: \n" + sbh.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -