不知道如何更好地解釋它,但我試圖從包含函數的URL(對吧?)獲取響應。從Web方法獲取java中的ajax響應(java.io.FileNotFoundException)
我正在爲此工作了很多小時,每次都有所進展,但無法完成此工作。 這是Chrome瀏覽器開發工具的請求和響應頭:
我的代碼是:
String params = "{\"prefixText\":\"" + city
+ "\",\"count\":10,\"contextKey\":\"he\"}";
conn = (HttpURLConnection) new URL(
"http://bus.gov.il/WebForms/wfrmMain.aspx/GetCompletionList")
.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setChunkedStreamingMode(0);
// conn.setFixedLengthStreamingMode(params.length());
conn.addRequestProperty("Accept", "*/*");
conn.addRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.addRequestProperty("Content-Length", String.valueOf(params.length()));
conn.addRequestProperty("Host", "bus.gov.il");
conn.addRequestProperty("Origin", "http://bus.gov.il");
conn.addRequestProperty("X-Requested-With", "XMLHttpRequest");
conn.addRequestProperty("Referer",
"http://bus.gov.il/WebForms/wfrmMain.aspx?width=1024&company=1&language=he&state=");
OutputStream os = new BufferedOutputStream(conn.getOutputStream());
os.write(params.getBytes());
String answer = readStream(conn.getInputStream());
調用「的getInputStream」當我得到的異常(我在堆棧跟蹤看到)在這條線:
String answer = readStream(conn.getInputStream());
進入readStream功能之前!
我不知道如何解決它...
試圖尋找有關的XMLHttpRequest但認爲這只是在JS。
另外:我知道我有很多不必要的請求屬性,但我不知道哪些是不必要的,直到代碼將工作。 在此先感謝:)
任何需要的工作代碼: http://pastebin.com/kFZwA5zR – RonLut 2011-12-27 09:38:23