对WEB url 发送POST请求

摘要:
包com.excellence。火花导入java.util。列表importcom.excellence.spark.test。测验importcom.sun.xml.internal.bind.v2.schemage.xmlschema。进口importorg.apache.http。HttpResponse;进口包装。
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

免责声明:文章转载自《对WEB url 发送POST请求》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇oracle中有关用户、角色的一些概念。SpringMVC之编程式校验下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

浏览器和服务器 对http请求(post get) url长度限制

1. GET URL长度限制 在Http1.1协议中并没有提出针对URL的长度进行限制,RFC协议里面是这样描述的,HTTP协议并不对URI的长度做任何的限制,服务器端 必须能够处理任何它们所提供服务多能接受的URI,并且能够处理无限长度的URI,如果服务器不能处理过长的URI,那么应该返回414状态码。 虽然Http协议规定了,但是Web服务器和浏览器...

java发起post请求—— body有参/无参

import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpRespon...

【SQL注入】之SQLMAP工具的使用

(本文仅为平时学习记录,若有错误请大佬指出,如果本文能帮到你那我也是很开心啦) 一、介绍 1.SQL注入工具:明小子、啊D、罗卜头、穿山甲、SQLMAP等等 2.SQLMAP:使用python开发,开源自动化注入利用工具,支持12种数据库 ,在/plugins/dbms中可以看到支持的数据库种类,在所有注入利用工具中它是最好用的!!! 3.支持的注入类型:...

小程序开发笔记【三】,评论、评论回复及消息通知实现

先看实现的效果图 评论及评论回复实现 分析 评论和评论回复可以设计成1张表也可以设计成2张表,根据使用场景的不同选择,这里我将评论和回复表设计成一张表,表结构如下 CREATE TABLE `tb_post_newcomment` ( `post_comment_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '...

Nginx关闭危险的HTTP请求

在 server { listen 80; server_name 127.0.0.1 www.baidu.com; 下一行添加一下代码即可关闭除GET|POST|HEAD外的请求方式。 if ($request_method !~* GET|POST|HEAD) {...

Request method 'GET' not supported

Request method 'GET' not supported 错误原因: GET请求不被允许。 解决方法: 1.从客户端入手。假设浏览器中的js用了ajax发起异步请求GET,将GET改为POST。 2.从服务端入手。Controller层的  @RequestMapping( method  =RequestMethod.POST ),将POST...