zoukankan      html  css  js  c++  java
  • 向服务器发送 http post 请求

    erlang的httpc来向server端发送post请求,实例如下:

    -module(t).   

    -export([t/0]).  

    -define(Name,"host").  

    -define(Passwd,112233).  

    t()->  

        inets:start(),  

        ssl:start(),  

        case httpc:request(post,{"http://192.168.2.185:8080/login",  

            [],"application/x-www-form-urlencoded",  lists:concat(["username=" ,Name ,"&password=" ,Passwd])},[],[]) of   

            {ok, {_,_,Body}}-> Body;  

            {error, Reason}->io:format("error cause ~p~n",[Reason])  

        end.  

    通过文档可以了解到httpc:request/4的使用方法。如果request方法的参数填对就可以得到{ok,Result}, Result -> 

    {status_line(), headers(), Body} | {status_code(), Body} | request_id() },

    这里得到的Result 为 {status_line(), headers(), Body},故上面是只取了需要的Body。即Boby为请求的返回值。

    这里要注意的是Body的返回值是否符合期望,关联的是request 所提交的Url,键值等等内容是否和后端达成交

    互,true -> 期望值;false -> [ ]。

    这里需要注意的是httpc模块下的request在几个参数下是如何实现的,(request/1,2,4,5)之前的介绍过request/1的实现.

    在查看了文档后,request/4 如下:

    request(Method, Request, HTTPOptions, Options)  -> {ok, Result} | {error, Reason} ,

    对请求参数类型解释如下

    Types:

    Method = method() ,   

                                  method()    = head | get | put | post | trace | options | delete

    Request = request() ,

                       而  request()       = {url(), headers()} |
                                                                    {url(), headers(), content_type(), body()}

    对返回参数类型解释如下:

    Types:

    Result = {status_line(), headers(), body()} | {status_code(), body()} | request_id()

    其实,上述即在erlang的shell中,1> inets:start(). 

    2> httpc:request("http:www.baidu.com"). 

  • 相关阅读:
    读书笔记之理想设计的特征
    一些javascript 变量声明的 疑惑
    LINQ 使用方法
    Google MySQL tool releases
    读书笔记之设计的层次
    EF之数据库连接问题The specified named connection is either not found in the configuration, not intended to be used with the Ent
    转载 什么是闭包
    javascript面向对象起步
    Tips
    数据结构在游戏中的应用
  • 原文地址:https://www.cnblogs.com/unqiang/p/3080222.html
Copyright © 2011-2022 走看看