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请求的更多相关文章

  1. 向指定URL 发送POST请求的方法

    java发送psot请求: package com.tea.web.admin; import java.io.BufferedReader; import java.io.IOException; ...

  2. python爬微信公众号前10篇历史文章(2)-拼接URL&发送http请求

    如何拼接想要的url http://weixin.sogou.com/weixin?type=1&page=1&ie=utf8&query=%E5%A4%A7%E7%BA%BD ...

  3. java通过java.net.URL发送http请求调用接口

    一般在*.html,*.jsp页面中我们通过使用ajax调用接口,这个是我们通常用的.对于这些接口,大都是本公司写的接口供自己调用,所以直接用ajax就可以.但是,如果是多家公司共同开发一个东西,一个 ...

  4. Hbuilder MUI里面使用java.net.URL发送网络请求,操作cookie

    1. 引入所需网络请求类: var URL = plus.android.importClass("java.net.URL"); var URLConnection = plus ...

  5. Ajax在静态页面中向指定url发送json请求获取返回的json数据

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  6. 【jQuery】JS中拼接URL发送GET请求的中文、特殊符号的问题

    > 参考的优秀文章 jQuery ajax - param() 方法 经常,我们需要在JS中拼接URL然后以GET形式提交请求.如果遇到中文.特殊符号则需要作各种处理. jQuery有一个方法非 ...

  7. HTTP 笔记与总结(3 )socket 编程:发送 GET 请求

    使用 PHP + socket 模拟发送 HTTP GET 请求,过程是: ① 打开连接 ② 构造 GET 请求的数据:写入请求行.请求头信息.请求主体信息(GET 请求没有主体信息) ③ 发送 GE ...

  8. JAVA使用apache http组件发送POST请求

    在上一篇文章中,使用了JDK中原始的HttpURLConnection向指定URL发送POST请求 可以看到编码量有些大,并且使用了输入输出流 传递的参数还是用“name=XXX”这种硬编的字符串进行 ...

  9. socket编程发送GET请求

    可以根据几根url地址,分析出主机,地址,协议等,然后用封装成的类拼接成GET请求信息,用fsockopen连接主机,进行读取操作,获取响应信息,打印 <?php //http连接接口 inte ...

随机推荐

  1. 使用jieba导入引用方法时,报错AttributeError: module 'jieba' has no attribute 'cut'

    一.问题描述 import jieba导入后,使用jieba.cut()方法时报错AttributeError: module 'jieba' has no attribute 'cut' 二.问题分 ...

  2. LaTeX表格绘制备忘之Go语言中的几个表

      以下绘制的表格选自<Go语言 云动力>一书.这些表格比较简单,LaTeX语句也比较简单.   完整代码: % 博客园陆巍的博客 https://www.cnblogs.com/atth ...

  3. C# 获取北京时间 (根据纪元时间(1970/1/1)转换为DateTime)

    根据纪元时间(1970/1/1)转换为DateTime WebClient wc = new WebClient(); s= wc.DownloadString("http://api.ti ...

  4. 【LG3240】[HNOI2015]实验比较

    题面 洛谷 题解 30pts 爆搜即可. 100pts 题意描述里有一句:"对每张图片\(i\),小\(D\)都最多只记住了某一张质量不比\(i\)差的另一张图片\(K_i\)." ...

  5. Spring的IOC理解(转载)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  6. Python中re模块详细介绍

    正则的介绍及应用实例详解 """ 1.什么是正则 正则就是用一系列具有特殊含义的字符组成一套规则,该规则用来描述具有某一特征的字符串, 正则就是用来去一个大的字符串中匹配 ...

  7. 应用.NET控制台应用程序开发批量导入程序。

    一.最近一直在调整去年以及维护去年开发的项目,好久没有在进行个人的博客了.每天抽了一定的时间在研究一些开源的框架,Drapper 以及NHibernate以及当前比较流行的SqlSuper框架 并进行 ...

  8. 在云服务器搭建WordPress博客(一)实现云服务器与域名的绑定

    随着云的兴起,越来越多的人选择在云服务器上搭建自己的博客,比较著名的开源博客管理系统当属WordPress了,那么怎么在服务器上搭建WordPress呢? 我们需要让别人能够访问我们的博客,就比如输入 ...

  9. $('#uplodFileForm')[0].submit();

    jquery对象在[0]以下是取其相对应的Dom对象,即$("#mainForm")[0] = document.getElementById("mainForm&quo ...

  10. Google 日历短信通知没有了

    关于 Google 日历短信通知的重要通知 从 2015 年 6 月 27 日起,Google 日历将不再发送短信通知.短信通知是我们在智能手机问世之前推出的功能.如今,智能手机和通知随处可见,即使处 ...