首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTTP/1.1 400错误请求400

HTTP/1.1 400错误请求400
EN

Stack Overflow用户
提问于 2017-03-02 14:45:13
回答 1查看 1.1K关注 0票数 0

我有我的基于Rest的服务,我必须将XML传递给它,当我在RestClient和postman中运行它时,它运行正常,但当我在代码中运行它时,它给我的400坏请求代码在下面给出了任何种类的帮助都会很有帮助。谢谢

代码语言:javascript
复制
public static void callservice() throws URISyntaxException {
    String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
    HttpClient httpClient = new DefaultHttpClient();
    try {

        HttpPost post = new HttpPost();
        URI as = new URI("http://172.24.1.137:8280/finalApi/v1.0.0");

        post.setURI(as);
        post.setHeader("Authorization",
                "Bearer 62fbb099-af9c-35a4-b8e5-82adf92ae9bd");
        post.setHeader("SOAPAction",
                "http://tempuri.org/IService/accountbalance");
        post.setHeader("content-type", "text/xml; charset=utf-8");
        post.setHeader("cache-control", "no-cache");

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        StringBuffer xmlString = new StringBuffer();

        xmlString
                .append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">");
        xmlString.append("<soapenv:Header/>");
        xmlString.append("<soapenv:Body>");
        xmlString.append("<tem:accountbalance>");
        xmlString.append("<tem:accountNo>051000207960141</tem:accountNo>");
        xmlString.append("</tem:accountbalance>");
        xmlString.append("</soapenv:Body>");
        xmlString.append("</soapenv:Envelope>");
        String stw = new String(
                "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\r\n   <soapenv:Header/>\r\n   <soapenv:Body>\r\n      <tem:accountbalance>\r\n         <!--Optional:-->\r\n         <tem:accountno>051000207960141</tem:accountno>\r\n      </tem:accountbalance>\r\n   </soapenv:Body>\r\n</soapenv:Envelope>");
        urlParameters.add(new BasicNameValuePair("xmldoc", xmlString
                .toString()));

        System.out.println(xmlString);
        // urlParameters.add(new BasicNameValuePair("xml",
        // xmlString.toString()));
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        // Execute HTTP request

        HttpResponse httpResponse = httpClient.execute(post);

        System.out.println("----------------------------------------");
        System.out.println(httpResponse.getStatusLine());
        System.out.println(httpResponse.getStatusLine().getStatusCode());
        System.out.println(httpResponse.getParams());
        System.out.println("----------------------------------------");

        // Get hold of the response entity
        HttpEntity entity = httpResponse.getEntity();

        // If the response does not enclose an entity, there is no need
        // to bother about connection release
        byte[] buffer = new byte[1024];
        if (entity != null) {
            InputStream inputStream = entity.getContent();
            try {
                int bytesRead = 0;
                BufferedInputStream bis = new BufferedInputStream(
                        inputStream);
                while ((bytesRead = bis.read(buffer)) != -1) {
                    String chunk = new String(buffer, 0, bytesRead);
                    System.out.println(chunk);
                }
            } catch (IOException ioException) {
                // In case of an IOException the connection will be released
                // back to the connection manager automatically
                ioException.printStackTrace();
            } catch (RuntimeException runtimeException) {
                // In case of an unexpected exception you may want to abort
                // the HTTP request in order to shut down the underlying
                // connection immediately.
                post.abort();
                runtimeException.printStackTrace();
            } finally {
                // Closing the input stream will trigger connection release
                try {
                    inputStream.close();
                } catch (Exception ignore) {
                }
            }
        }
    } catch (ClientProtocolException e) {
        // thrown by httpClient.execute(httpGetRequest)
        e.printStackTrace();
    } catch (IOException e) {
        // thrown by entity.getContent();
        e.printStackTrace();
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpClient.getConnectionManager().shutdown();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-03-02 14:54:11

您需要在您的api所在的目标服务器上允许跨域请求。请看这个:- How to enable cross-domain request on the server?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42548560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档