如何使用 Java RESTful 客户端使用 Wisetrend OCR Web 服务?

发布于 2024-10-19 07:32:44 字数 4459 浏览 2 评论 0原文

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# 代码的帮助下想出了此代码。

http://www.wisetrend.com/WiseTREND_Online_OCR_API_v2.0.htm

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.

http://www.wisetrend.com/WiseTREND_Online_OCR_API_v2.0.htm

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2024-10-26 07:32:44

有问题的行是 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.

掩饰不了的爱 2024-10-26 07:32:44

我忘记发送 JobUrlGET 请求。

现在工作正常。

以下代码将完成所需的工作。

HttpGet httpget = new HttpGet(sp.jobUrl);
System.out.println("Sending GET Request");
while(true)
{
HttpResponse response1 = httpClient.execute(httpget);
if(response1 != null)
{
ret = EntityUtils.toString(response1.getEntity());
                                    System.out.println("RES 1 : "+ret);
}
}

I forget to send a GET request for the JobUrl.

It is working fine now.

The following code will do the needful.

HttpGet httpget = new HttpGet(sp.jobUrl);
System.out.println("Sending GET Request");
while(true)
{
HttpResponse response1 = httpClient.execute(httpget);
if(response1 != null)
{
ret = EntityUtils.toString(response1.getEntity());
                                    System.out.println("RES 1 : "+ret);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文