zoukankan      html  css  js  c++  java
  • java请求python的x-www-form-urlencoded接口

    研究了半天= =|||终于成功了,python的post接口和java的不太一样,python不同的包的接口是不是一样不太了解。

    我这边请求的是tornado的post接口

    先加个依赖:

    gradle:         

    compile "commons-httpclient:commons-httpclient:3.1"

    spring:

    <dependency>

    <groupId>commons-httpclient</groupId>

    <artifactId>commons-httpclient</artifactId>

     <version>3.1</version>

    </dependency>

    java代码:

    //获得post结果字符串
        public String getResult(String text){
            String url = "改成你的请求地址"; //请求地址
            String reStr = "请求返回错误";
            try {
                reStr = sendPost(url, text);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return decodeUnicode(reStr);
        }
    
        private String sendPost(String postURL, String text) throws Exception{
            PostMethod postMethod = new PostMethod(postURL) ;
            postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
            //参数设置,需要注意的就是里边不能传NULL,要传空字符串
    NameValuePair[] data = {
            
    new NameValuePair("param1","test"),
            new NameValuePair("param2",text)    //post参数,pyhton的post接口的格式类似form表单
         };
            postMethod.setRequestBody(data);
            org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
            httpClient.executeMethod(postMethod);
            String result = postMethod.getResponseBodyAsString() ;
            return result;
        }
    
        // unicode -> 汉字  处理python接口可能出现的unicode乱码   要是没有汉字乱码问题就不用
        public static String decodeUnicode(String theString) {
            char aChar;
            int len = theString.length();
            StringBuffer outBuffer = new StringBuffer(len);
            for (int x = 0; x < len;) {
                aChar = theString.charAt(x++);
                if (aChar == '\') {
                    aChar = theString.charAt(x++);
                    if (aChar == 'u') {
                        // Read the xxxx
                        int value = 0;
                        for (int i = 0; i < 4; i++) {
                            aChar = theString.charAt(x++);
                            switch (aChar) {
                                case '0':
                                case '1':
                                case '2':
                                case '3':
                                case '4':
                                case '5':
                                case '6':
                                case '7':
                                case '8':
                                case '9':
                                    value = (value << 4) + aChar - '0';
                                    break;
                                case 'a':
                                case 'b':
                                case 'c':
                                case 'd':
                                case 'e':
                                case 'f':
                                    value = (value << 4) + 10 + aChar - 'a';
                                    break;
                                case 'A':
                                case 'B':
                                case 'C':
                                case 'D':
                                case 'E':
                                case 'F':
                                    value = (value << 4) + 10 + aChar - 'A';
                                    break;
                                default:
                                    throw new IllegalArgumentException(
                                            "Malformed   \uxxxx   encoding.");
                            }
    
                        }
                        outBuffer.append((char) value);
                    } else {
                        if (aChar == 't')
                            aChar = '	';
                        else if (aChar == 'r')
                            aChar = '
    ';
                        else if (aChar == 'n')
                            aChar = '
    ';
                        else if (aChar == 'f')
                            aChar = 'f';
                        outBuffer.append(aChar);
                    }
                } else
                    outBuffer.append(aChar);
            }
            return outBuffer.toString();
        }
  • 相关阅读:
    Abp 领域事件简单实践 <一>
    Abp session和Cookie
    Abp 聚合测试
    反向代理
    Angular 变更检测
    VS 之github
    Docker 启动SQLServer
    查出第二大的数是多少
    DDD 理解
    ActionBar的简单应用
  • 原文地址:https://www.cnblogs.com/clamp7724/p/13948935.html
Copyright © 2011-2022 走看看