0
我正在嘗試進行GET請求以獲取從銀行連接返回的字符串形式的XML文檔,但我嘗試了很多示例,並且所得到的結果都是錯誤。如何發送使用此格式的GET請求JAVA
HTTP GET
以下是示例HTTP GET請求和響應。顯示的佔位符需要用實際值替換。
GET /indicadoreseconomicos/WebServices/wsIndicadoresEconomicos.asmx/ObtenerIndicadoresEconomicosXML?tcIndicador=string&tcFechaInicio=string&tcFechaFinal=string&tcNombre=string&tnSubNiveles=string HTTP/1.1
Host: indicadoreseconomicos.bccr.fi.cr
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ws.sdde.bccr.fi.cr">string</string>
我將參數替換爲必要的,但沒有運氣。 我已採取的信息: http://www.bccr.fi.cr/indicadores_economicos_/ServicioWeb.html
代碼我試過了。
public String requestRate() {
String date = new SimpleDateFormat("dd/mm/yyyy").format(new Date());
String response = "";
Socket socket = new Socket(defaultURL, 8080);
String request = "GET /indicadoreseconomicos/WebServices/wsIndicadoresEconomicos.asmx/"
+ "ObtenerIndicadoresEconomicosXML?tcIndicador="
+ tcIndicador+ "&tcFechaInicio="
+ date + "&tcFechaFinal="
+ date + "&tcNombre=user&tnSubNiveles=N HTTP/1.1\r\n";
OutputStream os = socket.getOutputStream();
os.write(request.getBytes());
os.flush();
InputStream is;
is = socket.getInputStream();
int ch;
while ((ch = is.read()) != -1) {
response += (char) ch;
socket.close();
return response;
}
private String defaultURL = "http://www.indicadoreseconomicos.bccr.fi.cr";
我已經刪除了try-catch塊以方便閱讀。
可以分享你的代碼 –
不知道你用什麼來發出GET請求,但[Apache HttpComponents的HttpClient](http://hc.apache.org/httpcomponents-client-ga/)很漂亮Java中的標準。 – superEb
我明白了,但是我必須在沒有外部庫的情況下製作它。 – ditmark12