对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
对WEB url 发送POST请求的更多相关文章
- 向指定URL 发送POST请求的方法
java发送psot请求: package com.tea.web.admin; import java.io.BufferedReader; import java.io.IOException; ...
- python爬微信公众号前10篇历史文章(2)-拼接URL&发送http请求
如何拼接想要的url http://weixin.sogou.com/weixin?type=1&page=1&ie=utf8&query=%E5%A4%A7%E7%BA%BD ...
- java通过java.net.URL发送http请求调用接口
一般在*.html,*.jsp页面中我们通过使用ajax调用接口,这个是我们通常用的.对于这些接口,大都是本公司写的接口供自己调用,所以直接用ajax就可以.但是,如果是多家公司共同开发一个东西,一个 ...
- Hbuilder MUI里面使用java.net.URL发送网络请求,操作cookie
1. 引入所需网络请求类: var URL = plus.android.importClass("java.net.URL"); var URLConnection = plus ...
- Ajax在静态页面中向指定url发送json请求获取返回的json数据
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 【jQuery】JS中拼接URL发送GET请求的中文、特殊符号的问题
> 参考的优秀文章 jQuery ajax - param() 方法 经常,我们需要在JS中拼接URL然后以GET形式提交请求.如果遇到中文.特殊符号则需要作各种处理. jQuery有一个方法非 ...
- HTTP 笔记与总结(3 )socket 编程:发送 GET 请求
使用 PHP + socket 模拟发送 HTTP GET 请求,过程是: ① 打开连接 ② 构造 GET 请求的数据:写入请求行.请求头信息.请求主体信息(GET 请求没有主体信息) ③ 发送 GE ...
- JAVA使用apache http组件发送POST请求
在上一篇文章中,使用了JDK中原始的HttpURLConnection向指定URL发送POST请求 可以看到编码量有些大,并且使用了输入输出流 传递的参数还是用“name=XXX”这种硬编的字符串进行 ...
- socket编程发送GET请求
可以根据几根url地址,分析出主机,地址,协议等,然后用封装成的类拼接成GET请求信息,用fsockopen连接主机,进行读取操作,获取响应信息,打印 <?php //http连接接口 inte ...
随机推荐
- 解决SDL/SDL.h: No such file or directory
我在使用SDL2的时候遇到始终找不到头文件的问题,到处百度然后我尝试使用#include<SDL2/SDL.h>编译通过了,那么我很显然我之前设置的环境变量应该是没有生效的,后面在返回来研 ...
- rails 中http请求发生access-control-allow-origin错误
在api项目中 本地项目无法访问服务器api 百度了下,查出原因 接着找到rails项目的解决方法,安装rack-cors这个gem包 具体方法如下: Gemfile中加入 gem 'rack-cor ...
- 你的程序运行使用了多少CPU,秒知!
朋友们,相信大家日夜操练,代码已经撸了不少了,在跟代码打交道的时候,大家有没有思考过一个问题,想过你的代码完成一个循环或者处理其它事件它到底花了多少时间吗? “什么,你不是装逼吧,居然还可以知道代码运 ...
- [转载]C#委托与事件--简单笔记
原文地址:https://www.cnblogs.com/joeymary/p/8486358.html 委托 简单记录点东西 适合似懂非懂的朋友看看委托类型用来定义和响应应用程序中的回调.借此可以设 ...
- 关于自动AC机
嗯,,,,自动AC机 在cena评测时: Const SourcePath:string='incantation'; InputFile:string='incantation.in'; Outpu ...
- Velocity学习3
Velocity快速入门教程 Apache的速度是一个基于Java的的模板引擎(模板引擎),它允许任何人仅仅简单的使用模板语言(模板语言)来引用由java的代码定义的对象. 官网介绍如下: Veloc ...
- Windows和Linux下通用的线程接口
对于多线程开发,Linux下有pthread线程库,使用起来比较方便,而Windows没有,对于涉及到多线程的跨平台代码开发,会带来不便.这里参考网络上的一些文章,整理了在Windows和Linux下 ...
- 编译、安装rdesktop 1.8.3
来自:https://blog.csdn.net/songshimvp1/article/details/48290885 1.安装GCC: 安装C/C++编译器apt-get install gcc ...
- python并发编程之多进程理论知识
一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...
- Python: C扩展初体验
前言 使用 Python 毋庸置疑减少了很多规则约束和开发成本,让我们能够更加专注于逻辑而非语法.但是得此失彼,开发效率提高了,却带来了运行性能的问题,所以就常常被其他门派追着暴打. 身为一个 pyt ...