各种开发语言示例调用HTTP接口(示例中默认HTTP接口编码为gb2312)
asp示例:
function getHTTPPage(strurl,data)
on error resume next
set http = Server.CreateObject("Msxml2.XMLHTTP")
http.Open "POST",strurl, false
http.setRequestHeader "Content-type:", "text/xml;charset=GB2312"
Http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.Send(data)
getHTTPPage=http.ResponseText
set http=nothing
end function
C#示例:
public static string PostData(string purl,string str)
{
try
{
byte[] data = System.Text.Encoding.GetEncoding("GB2312").GetBytes(str);
// 准备请求
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(purl);
//设置超时
req.Timeout = 30000;
req.Method = "Post";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
Stream stream = req.GetRequestStream();
// 发送数据
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
Stream receiveStream = rep.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
StringBuilder sb = new StringBuilder("");
while (count > 0)
{
String readstr = new String(read, 0, count);
sb.Append(readstr);
count = readStream.Read(read, 0, 256);
}
rep.Close();
readStream.Close();
return sb.ToString();
}
catch (Exception ex)
{
return "posterror";
}
}
Delphi示例:
function HTTPwebservice(url:string):string;
var
responseText: WideString;
xmlHttp: OLEVariant;
begin
try
xmlHttp:=CreateOleObject('Msxml2.XMLHTTP');
xmlHttp.open('GET',url,false);
xmlHttp.send();
responseText:=xmlHttp.responseText;
if xmlHttp.status='200' then
begin
HTTPwebservice:=responseText;
end;
xmlHttp := Unassigned;
except
exit;
end;
end;
JAVA示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* 发送短信基础类
* @author administration
*
*/
public class SmsBase {
private String x_id="test";
private String x_pwd="123456";
public String SendSms(String mobile,String content) throws UnsupportedEncodingException{
Integer x_ac=10;//发送信息
HttpURLConnection httpconn = null;
String result="-20";
String memo = content.length()<70?content.trim():content.trim().substring(0, 70);
StringBuilder sb = new StringBuilder();
sb.append("URL?");
sb.append("id=").append(x_id);
sb.append("&pwd=").append(x_pwd);
sb.append("&to=").append(mobile);
sb.append("&content=").append(URLEncoder.encode(content, "gb2312"));
try {
URL url = new URL(sb.toString());
httpconn = (HttpURLConnection) url.openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(httpconn.getInputStream()));
result = rd.readLine();
rd.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(httpconn!=null){
httpconn.disconnect();
httpconn=null;
}
}
return result;
}
}
PHP示例:
<?php
Function SendSMS($url,$postStr)
{
$ch = curl_init();
$header = "Content-type: text/xml; charset=gb2312";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
$str = mb_convert_encoding($result, "gb2312", "utf-8");
Return $str;
}
function sms_send($phone, $content) {
$phoneList = $phone;
$content = iconv("UTF-8","GB2312",$content); //如果乱码换成这一句,两者取其一即可urlencode($content);
$userName = urlencode('帐号');
$userPwd = '密码';
$url = "URL?";
$postStr = "id=".$userName."&pwd=".$userPwd."&to=".$phoneList."&content=".$content."&time=";
$res = SendSMS($url,$postStr);
return substr($res,0,3) == 000 ? true : strval($content);
}
echo sms_send('13129544822','短信内容');
?>
或者
<?php
$content= urlencode("你好啊短信内容");//内容
$uc=urlencode('帐号');
$pwd='密码';
$msgid="12";
$callee="13129544822";//手机号
$sendurl="URL?";
$sdata="uc=".$uc."&pwd=".$pwd."&callee=".$callee."&cont=".$content."&msgid=".$msgid."&otime=";
$xhr=new COM("MSXML2.XMLHTTP");
$xhr->open("POST",$sendurl,false);
$xhr->setRequestHeader ("Content-type:", "text/xml;charset=utf-8");
$xhr->setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
$xhr->send($sdata);
echo $xhr->responseText;
?>
上面两个是Windows—POST示例,下面这个是Linux-POST示例
<?php
function SendSMS($strMobile,$content){
$url="URL?id=%s&pwd=%s&to=%s&content=%s&time=";
$id = urlencode("账号");
$pwd = urlencode("密码");
$to = urlencode($strMobile);
$content = iconv("UTF-8","GB2312",$content); //将utf-8转为gb2312再发
$rurl = sprintf($url, $id, $pwd, $to, $content);
//初始化curl
$ch = curl_init() or die (curl_error());
//设置URL参数
curl_setopt($ch,CURLOPT_URL,$rurl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行请求
$result = curl_exec($ch) ;
//取得返回的结果,并显示
echo $result;
echo curl_error($ch);
//关闭CURL
curl_close($ch);
}
SendSMS($strMobile,$content);
?>
VB.NET示例:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'/注意引入com microsoft.xml.3.0
Dim Send_URL, Http, getHTTPPage
Send_URL = "URL?id=" + Trim(uid.Text) + "&pwd=" + Trim(pwd.Text) + "&to=" + mob.Text + "&content=" + msg.Text + "&time="
Http = CreateObject("MSXML2.XMLHTTP")
Http.Open("get", Send_URL, False)
Http.send()
getHTTPPage = Http.responseText
backinfo.Text = getHTTPPage
End Sub
Python示例:
import sys
import http
import urllib.request
import urllib.parse
import re
import time
class SendMessage:
def main():
url = 'URL?'
uid = '帐号'
pwd = '密码'
phone = input("Input the target telphone number:")
content = input("Message:")
url_address = url + 'id=' + urllib.parse.quote(uid.encode('gb2312')) + "&pwd=" + pwd + "&to=" + phone + "&content=" + urllib.parse.quote(content.encode("gb2312")) + "&time="
answer = urllib.request.urlopen(url_address)
data = answer.read()
print(data)
if __name__ == "__main__":
main()
VB示例:
Private Sub Command1_Click()
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "POST", "URL?", False
objXMLHTTP.setRequestHeader "Content-Type", "text/xml;charset=GB2312"
objXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
sdata = "id=" & uid & "&pwd=" & pwd & "&to=" & mobiles & "&content=" & message & "&time=" sdata = URLEncoding(sdata)
objXMLHTTP.send (sdata)
返回值 = objXMLHTTP.ResponseText
Set objXMLHTTP = Nothing
End Sub
易语言示例:
.程序集 窗口程序集1
.子程序 _发送_被单击
.局部变量 obj, 文本型
.局部变量 http, 对象
' 如果群发,手机号码格式以","隔开,结果返回000表示发送成功
obj = “URL?id=” + 账户编辑.内容 + “&pwd=” + 密码编辑.内容 + “&to=” + 手机号码编辑.内容 + “&content=” + 内容编辑.内容 + “&time=”
.如果真 (http.创建 (“Microsoft.XMLHTTP”, ))
http.方法 (“open”, “GET”, obj, 假)
http.写属性 (“Content-type”, “text/xml; charset=gb2312”)
http.方法 (“send”, )
编辑框1.内容 = 到文本 (http.读文本属性 (“responseText”, ))
各种开发语言示例调用HTTP接口(示例中默认HTTP接口编码为gb2312)的更多相关文章
- python接口自动化中,注册接口随机生成手机号码
如大家所知在注册接口中,手机号参数需要的是未注册的手机号,而在测试用例中,你写入的手机号不一定是未注册的.所以这时需要对注册接口中传入的手机号做处理.下面我就分享一个课程里面学到的一个处理手机号的py ...
- ODI中的临时接口
在ODI 11g及后续的版本中,针对复杂的ETL处理,可分解为多个步骤,在中间步骤中使用临时接口,而不用建立相应的物理表,ODI会在处理过程中自动创建和删除这些中间表,从而降低ETL处理复杂度:同时, ...
- php中创建和调用webservice接口示例
php中创建和调用webservice接口示例 这篇文章主要介绍了php中创建和调用webservice接口示例,包括webservice基本知识.webservice服务端例子.webservi ...
- WCF服务开发与调用的完整示例
WCF服务开发与调用的完整示例 开发工具:VS2008 开发语言:C# 开发内容:简单的权限管理系统 第一步.建立WCF服务库 点击确定,将建立一个WCF 服务库示例程序,自动生成一个包括IServi ...
- 微信js-sdk开发获取签名和获取地理位置接口示例
###微信js-sdk开发获取签名和获取地理位置接口示例 前言:在做微信公众号开发时需要获取用户的地理位置信息,之前通过高德或者百度.腾讯等地图的api时发现经常获取不到,毕竟第三方的东西,后来改为采 ...
- C++开发python windows版本的扩展模块示例
C++开发python windows版本的扩展模块示例 测试环境介绍和准备 测试环境: 操作系统:windows10 Python版本:3.7.0 VS版本:vs2015社区版(免费) 相关工具下载 ...
- 客户端负载均衡Feign之一:申明式服务调用Feign入门示例
Spring Cloud提供了Ribbon和Feign作为客户端的负载均衡. 前面使用了Ribbon做客户端负载均衡,使用Hystrix做容错保护,这两者被作为基础工具类框架被广泛地应用在各个微服务的 ...
- 【嵌入式开发】 嵌入式开发工具简介 (裸板调试示例 | 交叉工具链 | Makefile | 链接器脚本 | eclipse JLink 调试环境)
作者 : 韩曙亮 博客地址 : http://blog.csdn.net/shulianghan/article/details/42239705 参考博客 : [嵌入式开发]嵌入式 开发环境 (远 ...
- 【Android 应用开发】Android 图表绘制 achartengine 示例解析
作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/38420197 一. AChartEngine 简介 1. 项 ...
随机推荐
- 【转】C#正则表达式详解
正则表达式通常包含字母文本(Literaltext)和元字符(metacharacter) 字母文本指的是普通文本如"abcde"可匹配字符串中任何包含"abcde&qu ...
- 原生JS写Ajax的请求函数
一.JS原生ajax ajax:一种请求数据的方式,不需要刷新整个页面:ajax的技术核心是 XMLHttpRequest 对象:ajax 请求过程:创建 XMLHttpRequest 对象.连接服务 ...
- 转: 静态模式makefile中$(cobjs): $(obj)/%.o: $(src)/%.c
4.12 静态模式静态模式规则是这样一个规则:规则存在多个目标,并且不同的目标可以根据目标文件的名字来自动构造出依赖文件.静态模式规则比多目标规则更通用,它不需要多个目标具有相同的依赖.但是静态模式规 ...
- BZOJ 1028 麻将
Description 麻将是中国传统的娱乐工具之一.麻将牌的牌可以分为字牌(共有东.南.西.北.中.发.白七种)和序数牌(分为条子.饼子.万子三种花色,每种花色各有一到九的九种牌),每种牌各四张.在 ...
- [转]Uploading and Downloading VHDs to Windows Azure
The article shows how to download and upload VHD to Azure. http://michaelwasham.com/windows-azure-po ...
- java中String s="abc"及String s=new String("abc")详解
1. 栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. 2. 栈的优势是,存取速度比堆要快,仅次于直 ...
- Android Wear开发 - 数据通讯 - 第二节 : 数据的发送与接收
本节由介绍3种数据的发送接收:1.Data Items : 比特类型数据,限制100KB以内2.Assets : 资源类型数据,大小无上限3.Message : 发送消息,触发指令 http://de ...
- 1027. Colors in Mars (20) PAT
题目:http://pat.zju.edu.cn/contests/pat-a-practise/1027 简单题,考察十进制数和n进制数的转换和输出格式的控制. People in Mars rep ...
- 在InnoDB,记录在 non-clustered indexes(也被称为secondary indexes) 包含了主键值
In InnoDB, the records in non-clustered indexes (also called secondary indexes) contain the primary ...
- Sublime Text 3 配置
配置: { "font_face": "Source Code Pro", , "highlight_line": true, " ...