如何使用 Java RESTful 客户端使用 Wisetrend OCR Web 服务?
import java.io.BufferedWriter;
import java.io.FileWriter;
import javax.xml.parsers.DocumentBuilder;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
public class OCRRestClient {
public static String url = "http://www.wisetrend.com/WiseTREND_Online_OCR_API_v2.0.htm";
private static int proxyPort=8000;
private static String proxyHost="ProxyHost";
static SAXParser sp;
public static void main(String[] args) {
OCRRestClient client = new OCRRestClient();
/* client.getConvertedText();
}
public void getConvertedText(){*/
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
DefaultHttpClient httpClient = new DefaultHttpClient(myParams);
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
try {
String ret = null;
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109);
String key = "****"; // my key
String imageURL = "http://www.androidpeople.com/wp-content/themes/bp-columns/images/android_logo.png";
HttpResponse response = null;
HttpPost httppost = new HttpPost("http://svc.webservius.com/v1/wisetrend/wiseocr/submit?wsvKey="+key);
httppost.setHeader("Content-Type","text/xml");
httppost.setEntity(new StringEntity("<Job>" +
"<InputURL>"+imageURL+"</InputURL>"+
"</Job>"));
response = httpClient.execute(httppost );
System.out.println(response.toString());
while(true){
sp = new SAXParser();
if (response != null) {
ret = EntityUtils.toString(response.getEntity());
System.out.println("Response: "+ret);
int success = writeFile(ret, "c://out.xml");
if(success == 1)
{
//parsing the document using SAX Parser.
sp.parseDocument("c://out.xml");
System.out.println("Final status : "+sp.status);
if(sp.status.equalsIgnoreCase("Finished")){
System.out.println("Finished status : "+sp.status);
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static int writeFile(String content, String filename)
{
try{
// Create file
FileWriter fstream = new FileWriter(filename);
BufferedWriter out = new BufferedWriter(fstream);
out.write(content);
//Close the output stream
out.close();
return 1;
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
return 0;
}
}
}
我在尝试访问此内容时收到以下错误。
org.apache.http.message.BasicHttpResponse@1837697
Response: <JobStatus xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><JobURL>http://api.ocr-it.com/ocr/v2/getStatus/f78377d5bdcd44d18cafe65c253f87e6</JobURL><Status>Submitted</Status></JobStatus>
Strt : Status:
End : Status: Submitted
java.lang.IllegalStateException: Content has been consumed
at org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:84)
at org.apache.http.conn.BasicManagedEntity.getContent(BasicManagedEntity.java:87)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:138)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:183)
at OCRRestClient.main(OCRRestClient.java:62)
Final status : Submitted
请帮我解决这个问题。我哪里失踪了?在哪里更改我的代码?
我在明智趋势文档页面中提供的 C# 代码的帮助下想出了此代码。
import java.io.BufferedWriter;
import java.io.FileWriter;
import javax.xml.parsers.DocumentBuilder;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
public class OCRRestClient {
public static String url = "http://www.wisetrend.com/WiseTREND_Online_OCR_API_v2.0.htm";
private static int proxyPort=8000;
private static String proxyHost="ProxyHost";
static SAXParser sp;
public static void main(String[] args) {
OCRRestClient client = new OCRRestClient();
/* client.getConvertedText();
}
public void getConvertedText(){*/
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
DefaultHttpClient httpClient = new DefaultHttpClient(myParams);
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
try {
String ret = null;
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109);
String key = "****"; // my key
String imageURL = "http://www.androidpeople.com/wp-content/themes/bp-columns/images/android_logo.png";
HttpResponse response = null;
HttpPost httppost = new HttpPost("http://svc.webservius.com/v1/wisetrend/wiseocr/submit?wsvKey="+key);
httppost.setHeader("Content-Type","text/xml");
httppost.setEntity(new StringEntity("<Job>" +
"<InputURL>"+imageURL+"</InputURL>"+
"</Job>"));
response = httpClient.execute(httppost );
System.out.println(response.toString());
while(true){
sp = new SAXParser();
if (response != null) {
ret = EntityUtils.toString(response.getEntity());
System.out.println("Response: "+ret);
int success = writeFile(ret, "c://out.xml");
if(success == 1)
{
//parsing the document using SAX Parser.
sp.parseDocument("c://out.xml");
System.out.println("Final status : "+sp.status);
if(sp.status.equalsIgnoreCase("Finished")){
System.out.println("Finished status : "+sp.status);
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static int writeFile(String content, String filename)
{
try{
// Create file
FileWriter fstream = new FileWriter(filename);
BufferedWriter out = new BufferedWriter(fstream);
out.write(content);
//Close the output stream
out.close();
return 1;
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
return 0;
}
}
}
I'm getting the following error, while trying to access this.
org.apache.http.message.BasicHttpResponse@1837697
Response: <JobStatus xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><JobURL>http://api.ocr-it.com/ocr/v2/getStatus/f78377d5bdcd44d18cafe65c253f87e6</JobURL><Status>Submitted</Status></JobStatus>
Strt : Status:
End : Status: Submitted
java.lang.IllegalStateException: Content has been consumed
at org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:84)
at org.apache.http.conn.BasicManagedEntity.getContent(BasicManagedEntity.java:87)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:138)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:183)
at OCRRestClient.main(OCRRestClient.java:62)
Final status : Submitted
Please help me to sort this. Where am i missing? Where to change my code?
I've come up with this code with the help of the c# code provided in the wise trend document page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有问题的行是 System.out.println(response.toString()); 。这基本上读取了一次响应。由于它是直接断线的,因此无法重新读取。删除 SOP,它应该可以工作。
The offending line is System.out.println(response.toString()); . This essentially reads the response once. Since it is directly off the wire, it cannot be reread. Remoce the SOP and it should work.
我忘记发送
JobUrl
的GET
请求。现在工作正常。
以下代码将完成所需的工作。
I forget to send a
GET
request for theJobUrl
.It is working fine now.
The following code will do the needful.