小白调用HTTP接口

sanyuan35 2019-04-12 10:17:22
现在外公司给我们公司一个接口地址 http://tempuri.org/sms.jsp" http调用,用POST方法,这如何通过webService实现传数据和读数据??
小小白。。总是弄不明白这些。大神们,可不可以说明一下设计的思路。我去学习一下。谢谢。
...全文
333 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tiny~ 2019-04-14
  • 打赏
  • 举报
回复
不需要webservice,简单代码就能实现。 之前搞的一个发送短信的,给你参考下,同是小白,用小白的办法来做的:
 

            string formUrl = "http://wwwurl/MT.do?Username="+smsuser+"&Password="+smspwd+"&Mobile=" + mbphone.Text.Trim() + "&Content=" + HttpUtility.UrlEncode(smscontent.Text.Trim(), System.Text.Encoding.GetEncoding("GB2312")) + "";

           // MessageBox.Show(formUrl);
            //return;
            CookieContainer cookieContainer = new CookieContainer();

            // 设置提交的相关参数 
            HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;

            request.Method = "GET";
            request.KeepAlive = false;
            request.AllowAutoRedirect = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.CookieContainer = cookieContainer;

            HttpWebResponse SendSMSResponse = (HttpWebResponse)request.GetResponse();
            StreamReader SendSMSResponseStream = new StreamReader(SendSMSResponse.GetResponseStream());

            string strRespone = SendSMSResponseStream.ReadToEnd();

            if (strRespone == null )
            {
                MessageBox.Show("没有返回数据");
            }
            else if (strRespone=="-1")
            {
                MessageBox.Show("用户名或密码验证错误!");
            }
            else if (strRespone == "-2")
            {
                MessageBox.Show("发送短信余额不足!");
            }
            else if (strRespone == "-3")
            {
                MessageBox.Show("号码超容(单次发送不大于100个号码)!");
            }
            else if (strRespone == "-4")
            {
                MessageBox.Show("内容非法(内容含被过滤的关键字)");
            }
            else if (strRespone == "-5")
            {
                MessageBox.Show("内容超长");
            }
            else if (strRespone == "-6")
            {
                MessageBox.Show("密钥验证出错");
            }
            else if (strRespone == "-9")
            {
                MessageBox.Show("函数入参不正确(某参数为空或参数数据类型不正确)");
            }
            else if (strRespone == "0")
            {
                MessageBox.Show("短信已成功发送!");
            }
            else {
                MessageBox.Show("未知错误");
            }
            
            SendSMSResponse.Close();
            SendSMSResponseStream.Close();
stherix 2019-04-12
  • 打赏
  • 举报
回复
首先你要弄到他们接口的文档 比如请求地址,参数,body详细内容等东西
  • 打赏
  • 举报
回复
学习 http 下的开发技术,应该首先学会使用 http 调试器使用。例如 Fiddler。 上述代码,dataBytes 就是上行消息的消息体(你还可以增加消息头),resBytes 就是对方返回的结果。
  • 打赏
  • 举报
回复
跟 webservice 没半点关系!
var resBytes = new WebClient().UploadData(url, dataBytes);
  • 打赏
  • 举报
回复
VS中不是可以添加服务引用吗

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧