简体   繁体   English

http客户端的体系结构

[英]Architecture for http client

I am developing application, with http client, and I wonder to make some elegant issue. 我正在使用http客户端开发应用程序,但我想提出一些优雅的问题。 This is standard java http client whose work in background thread, and passing data by event's (witch realized by override methods). 这是标准的Java http客户端,其在后台线程中工作,并通过事件(通过重写方法实现的女巫)传递数据。 I have special class for background requests, that implements method sendRequest() 我有一个用于后台请求的特殊类,该类实现了sendRequest()方法

protected void sendRequest(final String url)  {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                URI website = null;
                try {
                    website = new URI(url);
                } catch (URISyntaxException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                HttpGet request = new HttpGet();
                request.setURI(website);
                HttpResponse response = null;
                try {
                    response = client.execute(request, httpContext);
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                if (response != null)
                {
                    HttpEntity entity = response.getEntity();
                    try {
                        InputStream is = entity.getContent();
                        if (Debug.isDebuggerConnected()==true)
                        {
                            String data = convertStreamToString(is);
                            int code = response.getStatusLine().getStatusCode();                          
                            if (httpEvent!=null)
                                httpEvent.HttpResponseArrived(data, code);
                        }
                        else
                            httpEvent.HttpResponseArrived(convertStreamToString(is),response.getStatusLine().getStatusCode());
                        }
                    catch (IOException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
            }
        });
        t.start();
    }

And also child class, for API to web server, wich have methods like that: 还有用于Web服务器API的子类,它们具有以下方法:

public void getSomeData(SomeParams param)
    {
        sendRequest("http://xxx.yy"+gson.toJson(param));
        httpEvent = new HttpHandler()
        {
            @Override
            public void HttpResponseArrived(String data, int code)
            {
                switch (code)
                {
                    case 200:
                        //some code
                        break;
                    case 401:
                        //some code
                        break;

                }
            }
        };
    }

And my question: how elegant to handle server errors, for example 401? 我的问题是:处理服务器错误(例如401)有多优雅? I need to do this in one place, in method that sending requests - sendRequest(). 我需要在一处发送请求的方法中执行此操作-sendRequest()。 At first sight it is very easy: just handle 401, and if it's because expired cookie - call method Login() (in my design, it's look like getSomeData). 乍一看,这很容易:只需处理401,如果是由于过期的cookie,则调用方法Login()(在我的设计中,它看起来像getSomeData)。 But I want, not just login again, I need to request data, that I failed to get because the error. 但是我不仅希望再次登录,还需要请求数据,因为该错误导致我无法获取数据。 Of course, I can implement calling Login() method in every switch, like this: 当然,我可以在每个开关中实现调用Login()方法,如下所示:

case 401:
                    {
                        Login(CurrentContext.AuthData.Login, CurrentContext.AuthData.Password);
                        break;
                    }

But the login event implemented in Login() method; 但是登录事件是通过Login()方法实现的; Also, I can just write sendRequest(string authdata), subscrube for HttpHandler and by recursion call method thats implements this code. 另外,我可以只编写sendRequest(string authdata),HttpHandler的subscrube并通过实现此代码的递归调用方法来编写。 But I thind, it's not very good decision. 但是我变瘦了,这不是一个很好的决定。 I really hope, that somebody already solve this problem, and there is the way, to turn it's in beautiful code! 我真的希望有人已经解决了这个问题,并且有办法将其转换为漂亮的代码! Thanks, if you could to read this to the end:) 谢谢,如果您能读到最后:)

As for answer not comment. 至于答案不予评论。 Try to use http client framework - I prefer Apache HTTPClient. 尝试使用http客户端框架-我更喜欢Apache HTTPClient。 It provides wide controll over request and responses. 它提供对请求和响应的广泛控制。 Moreover it supports most common methods like GET POST etc. Cookie management, redirection handling and SSL support is also provided. 此外,它支持最常用的方法,例如GET POST等。还提供Cookie管理,重定向处理和SSL支持。 Don't invent something that is already invented. 不要发明已经发明的东西。 HttpClient - use v4.x HttpClient-使用v4.x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM