我有一個雅加達下議院HttpClient的問題。
在我自己寫的HttpServer得到真正的請求之前,有一個請求是完全空的。這是第一個問題。
第一個問題解決了。這是由不必要的URLConnection引起的!
第二個問題是,有時請求數據中的http請求的第三或第四行之後結束:
爲什麼我會從Jakarta Commons HttpClient獲取空請求?
POST/HTTP/1.1
User-Agent: Jakarta Commons-HttpClient/3.1
Host: 127.0.0.1:4232
對於調試我使用的軸TCPMonitor中。在那裏,每件事情都很好,但卻是空的要求。
如何處理我流:
StringBuffer requestBuffer = new StringBuffer();
InputStreamReader is = new InputStreamReader(socket.getInputStream(), "UTF-8");
int byteIn = -1;
do {
byteIn = is.read();
if (byteIn > 0) {
requestBuffer.append((char) byteIn);
}
} while (byteIn != -1 && is.ready());
String requestData = requestBuffer.toString();
發現處理流的新途徑。我讀取所有標題參數並使用'content-length'來讀取發佈數據。
InputStream is = mySocket.getInputStream();
if (is == null) {
return;
}
BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// Read the request line
// ...
// ...
// Parse the header
Properties header = new Properties();
if (st.hasMoreTokens()) {
String line = in.readLine();
while (line != null && line.trim().length() > 0) {
int p = line.indexOf(':');
header.put(line.substring(0, p).trim().toLowerCase(), line.substring(p + 1).trim());
line = in.readLine();
}
}
// If the method is POST, there may be parameters
// in data section, too, read it:
String postLine = "";
if (method.equalsIgnoreCase("POST")) {
long size = 0x7FFFFFFFFFFFFFFFl;
String contentLength = header.getProperty("content-length");
if (contentLength != null) {
try {
size = Integer.parseInt(contentLength);
} catch (NumberFormatException ex) {
}
}
postLine = "";
char buf[] = new char[512];
int read = in.read(buf);
while (read >= 0 && size > 0 && !postLine.endsWith("\r\n")) {
size -= read;
postLine += String.valueOf(buf, 0, read);
if (size > 0) {
read = in.read(buf);
}
}
postLine = postLine.trim();
decodeParms(postLine, parms);
}
如何發送請求:
client.getParams().setSoTimeout(30000);
method = new PostMethod(url.getPath());
method.getParams().setContentCharset("utf-8");
method.setRequestHeader("Content-Type", "application/xml; charset=utf-8");
method.addRequestHeader("Connection", "close");
method.setFollowRedirects(false);
byte[] requestXml = getRequestXml();
method.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(requestXml)));
client.executeMethod(method);
int statusCode = method.getStatusCode();
有有人對你的想法如何解決這些問題的問題?
亞歷
當我刪除is.ready()時,閱讀時間太長。大約需要20秒才能回覆。 – alexvetter 2010-03-08 13:26:01
當您說'響應回來'時,是否需要20秒才能將請求xml讀入請求緩衝區? – simonlord 2010-03-08 14:06:47
整個請求在讀取請求xml併發送響應時需要約20秒的時間。 – alexvetter 2010-03-08 15:08:19