I'm trying to get a xml file as a stream to parse it after that. But I'm getting this error:
ERROR : missing parameters, action:, objectType:, objectTable:, objectId:
With this error, the xml file is like:
<ERROR>
<STATUS>ko</STATUS>
<OBJECT_TYPE/>
<OBJECT_TABLE/>
<OBJECT_ID/>
<APP_PATH/>
<FILE_CONTENT/>
<FILE_EXTENSION/>
<ERROR>
missing parameters, action:editObject, objectType:, objectTable:, objectId:
</ERROR>
<END_MESSAGE/>
</ERROR>
But when I access the actual link on my browser, I'm getting the right file:
<SUCCESS>
<STATUS>ok</STATUS>
<OBJECT_TYPE>resource</OBJECT_TYPE>
<OBJECT_TABLE>res_version_business</OBJECT_TABLE>
<OBJECT_ID>206</OBJECT_ID>
<APP_PATH>start</APP_PATH>
<FILE_CONTENT>UEsDBBQABgAIAAAAIQA5en1zywEAAGMIAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAA</FILE_CONTENT>
<FILE_EXTENSION>docx</FILE_EXTENSION>
<ERROR/>
<END_MESSAGE/>
</SUCCESS>
Here is my code:
public void sendHttpRequest(String theUrl, String postRequest) throws Exception {
URL UrlOpenRequest = new URL(theUrl);
System.out.println("UrlOpenRequest: "+ theUrl);
HttpURLConnection HttpOpenRequest = (HttpURLConnection)
UrlOpenRequest.openConnection();
HttpOpenRequest.addRequestProperty("Accept", "*/xml");
//HttpOpenRequest.setDoInput(true);
HttpOpenRequest.setDoOutput(true);
HttpOpenRequest.setRequestMethod("POST");
HttpOpenRequest.setRequestProperty("Accept", "*/xml");
if (!"none".equals(postRequest)) {
OutputStreamWriter writer = new
OutputStreamWriter(HttpOpenRequest.getOutputStream());
writer.write("fileContent=" + this.fileContentTosend + "&fileExtension=" + this.fileExtension);
writer.flush();
} else {
OutputStreamWriter writer = new
OutputStreamWriter(HttpOpenRequest.getOutputStream());
writer.write("foo=bar");
writer.flush();
}
System.out.println("INPUT STREAM: "+HttpOpenRequest.getInputStream().available());
this.parse_xml(HttpOpenRequest.getInputStream());
HttpOpenRequest.disconnect();
}
Any idea about the cause of this error?
Try this code and see what you get :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class Main
{
public static void main(String[] args)
throws Exception
{
new Main();
}
public Main()
{
try
{
String myUrl = "http://google.com/"; // change your url here
String results = doHttpUrlConnectionAction(myUrl);
System.out.println(results);
}
catch (Exception e)
{
//whatever error
}
}
private String doHttpUrlConnectionAction(String desiredUrl)
throws Exception
{
URL url = null;
BufferedReader reader = null;
StringBuilder stringBuilder;
try
{
url = new URL(desiredUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(30*1000);
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
return stringBuilder.toString();
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.