• 对WEB url 发送POST请求


    package com.excellence.spark;
    
    import java.util.List;
    import com.excellence.spark.test.test;
    import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Import;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
    import org.apache.http.conn.ssl.SSLSocketFactory;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    import net.sf.json.JSONObject;
    
    import java.io.IOException;
    import java.net.URLDecoder;
     
    /**    对指定url发送post的指定内容
     * @author 947
     *
     */
    public class HttpRequest {
        /**
         * 
         * @param url         url
         * @param content      发送post的内容
         * @return
         */
        public static String httpPost(String url,String content){
            DefaultHttpClient httpClient = new DefaultHttpClient();
            JSONObject jsonResult = new JSONObject();
          
            HttpPost method = new HttpPost(url);
            try {
                if (null != content) {
                    StringEntity entity = new StringEntity(content, "utf-8");
                    entity.setContentEncoding("UTF-8");
                    entity.setContentType("application/json");
                    method.setEntity(entity);
                }
                HttpResponse result = httpClient.execute(method);
    
                url = URLDecoder.decode(url, "UTF-8");
      
                String entity = EntityUtils.toString(result.getEntity());    // 拿到响应的实体的字符串
                
                System.out.println(entity);
                return entity;
                
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        
        public static void main(String[] args) {
            String url = "http://127.0.0.1:8000/index/";
            JSONObject jsonObject =  new JSONObject();
            jsonObject.put("word", "111111");
            String result = HttpRequest.httpPost(url, jsonObject.toString());  // 返回响应结果
            System.out.println(result);
        }
    }

    如何在python中拿到这个request然后并响应返回response呢???

    #-*-coding:utf-8-*-
    
    from importlib import reload
    import json
    from django.shortcuts import render
    from django.shortcuts import HttpResponse
    import requests
    from sendPostText.predict import CnnModel
    
    def index(request):
        cnn_model = CnnModel()
    
        received_json_data = json.loads(request.body)
        word = received_json_data['word']
    
        result = cnn_model.predict(word)
    
        data = {'word':result}
    
        data = json.dumps(data,ensure_ascii=False)  # 这里加入ensure_ascil=False保证中文不乱码
        response = HttpResponse(data,content_type="application/json,charset=UTF-8")
        return response
  • 相关阅读:
    bootstrap-15
    sqlserver 根据指定值截取字符串
    idea针对有外联jar包的项目如何编译成可运行的jar包
    中控考勤机超级管理员密码算法
    sql server 取多条数据的最大值
    bat 脚本定时删除备份文件
    Spyder汉化教程
    君荣一卡通软件mysql转sqlserver 教程
    office2016专业增强版激活密匙 (shell激活版)
    sql 获取当前时间的前一天,不加时分秒
  • 原文地址:https://www.cnblogs.com/yiduobaozhiblog1/p/9945039.html
Copyright © 2020-2023  润新知