Webservice HTTP

摘要:
方式二:HTTP方式调用两个测试工具1.POSTMan2.Fiddler3.本地测试什么都没有问题,部署到阿里云远程服务器,在本地用上面的调用工具,就提示500错误。原来web.config里面默认Post设置是不允许远程调用webservice,改变一下配置就可以了。LinkId=169433--˃下面是使用HTTP调用webservice的代码varurl="http://localhost:9000/WebServiceGSMTest.asmx/GetToken3";stringdata="Id=11111&name=222";varretBack=HttpHelpers.RequestData;publicclassHttpResponse{publicboolStatus;publicstringRetStr;publicExceptionException;}publicenumReqMethod{POST,GET,}///////////////////////////publicstaticHttpResponseRequestData{HttpWebRequestrequest=null;HttpWebResponseresponse=null;Streamstream1=null;StreamReadersr=null;HttpResponseres=newHttpResponse();try{request=WebRequest.Create;request.Method=method.ToString();if(header!=null){request.Headers=header;}request.ContentType=contentType;if{byte[]postdatabyte=Encoding.UTF8.GetBytes;request.ContentLength=postdatabyte.Length;request.KeepAlive=true;Streamstream;stream=request.GetRequestStream();stream.Write;//设置请求主体的内容stream.Close();}//接收响应response=request.GetResponse();stream1=response.GetResponseStream();sr=newStreamReader;stringstr=sr.ReadToEnd();res.Status=true;res.RetStr=str;}catch{res.Exception=ex;}finally{if(stream1!

由于项目需要:自己写了一个WebserviceDemo,把遇到的问题记下来。

方式一 :使用代理类来访问Webservice,此方式不讲解,感觉复杂(神坑)。

(生成的代理路径 C:UsersadminAppDataLocalTempFrameWork.WebService.DynamicWebServiceCalling.doAction5DD81ED96F54BCFBC7A4634BB01BE5CA.dll)

方式二:HTTP方式调用两个测试工具

Webservice HTTP第1张

1.POSTMan

Webservice HTTP第2张

2.Fiddler

Webservice HTTP第3张

3.本地测试什么都没有问题,部署到阿里云远程服务器,在本地用上面的调用工具,就提示500错误。

原来web.config里面默认Post设置是不允许远程调用webservice,改变一下配置就可以了。
如果想在远程可以正常调用,需要修改web.config,在system.web节下面加上下面一段话即可。

<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation targetFramework="4.0" />
    <webServices >
      <protocols >
        <add name="HttpSoap"/>
        <add name="HttpPost"/>
        <add name="HttpGet"/>
        <add name="Documentation"/>
      </protocols>
    </webServices>
  </system.web>
</configuration>

Webservice HTTP第4张

下面是使用HTTP调用webservice的代码

   var url = "http://localhost:9000/WebServiceGSMTest.asmx/GetToken3";
            string data = "Id=11111&name=222";
            var retBack = HttpHelpers.RequestData(url, data, ReqMethod.POST, null, "application/x-www-form-urlencoded");
 public class HttpResponse
    {
        public bool Status;
        public string RetStr;
        public Exception Exception;
    }
    public enum ReqMethod
    {
        POST,
        GET,
    }
 /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="method"></param>
        /// <param name="header"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public static HttpResponse RequestData(string url, string data, ReqMethod method, WebHeaderCollection header, string contentType = "application/json")
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream stream1 = null;
            StreamReader sr = null;
            HttpResponse res = new HttpResponse();
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = method.ToString();
                if (header != null)
                {
                    request.Headers = header;
                }
                request.ContentType = contentType;
                if (method == ReqMethod.POST)
                {
                    byte[] postdatabyte = Encoding.UTF8.GetBytes(data);
                    request.ContentLength = postdatabyte.Length;
                    request.KeepAlive = true;
                    Stream stream;
                    stream = request.GetRequestStream();
                    stream.Write(postdatabyte, 0, postdatabyte.Length); //设置请求主体的内容
                    stream.Close();
                }
                //接收响应
                response = (HttpWebResponse)request.GetResponse();
                stream1 = response.GetResponseStream();
                sr = new StreamReader(stream1);
                string str = sr.ReadToEnd();
                res.Status = true;
                res.RetStr = str;
            }
            catch (Exception ex)
            {
                res.Exception = ex;
            }
            finally
            {
                if (stream1 != null)
                {
                    stream1.Close();
                    stream1.Dispose();
                }
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                }
            }
            return res;
        }

免责声明:文章转载自《Webservice HTTP》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇c#WinForm中TeeChart控件的注册和使用php获取form表单单选按钮和复选框的值下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

DEP(数据执行保护)介绍

数据执行保护是一组软件和硬件技术,可以对内存执行额外检查,以帮助防止恶意代码在系统上运行。硬件实现DEP来检测从这些位置运行的代码,并在发现执行时抛出异常。此功能也称为非执行和执行保护。为了与DEP合作,AMD和微软共同设计并开发了AMD的新芯片功能“增强病毒防护”。[1] DEP的安全机制,即“数据执行保护”,是一种Windows安全机制,主要用于防止病毒...

Uni-app v-on监听事件

使用标记上的v-on监视事件。缩写为@click common click events方法:方法:{Focus(){console.log;},blur(){console.log;},confirm(){console.log;},click(){console.log;},tap(){console.log;},longpress(){console....

Activiti-个人任务

1.分配任务所有者1.1固定分配在业务流程建模期间指定固定任务所有者;在properties视图中,填写Assignee项作为任务所有者;注:通过固定分配方法,任务是逐步执行的,任务负责人将根据bpmn的配置分配给每个任务;1.2表达式分配1.2.1 UEL表达式Activiti使用UEL表达式,UEL是javaEE6...

安装qmake与环境变量解析

如果你已经有了qmake,可以跳过这里,请看10分钟学会使用qmake。手动安装qmake在手工连编Qt之前,下面这些环境变量必须被设置:QMAKESPEC这个必须设置为你所使用的系统的平台和编译器的组合。当编译完成时,qmake已经可以使用了。这里对添加环境变量时,是在path里头添加,还是new一个变量有点疑惑。而如果是new的话,当我们在为程序添加路径...

01 . 美团全链路监控CAT简介及部署

现任携程架构总监)领导基于Java开发的实时应用程序监控平台的设计。作为大众点评网的基本监控组件,AT为大众点评网业务线提供系统的性能指标、健康状态、基本警报等。如何有效定位故障并缩短故障。。。监控是运维工作中最重要的环节,吴启民也是开源实时监控系统CAT的作者。系统故障监控、业务指标监控、应用程序性能监控、用户行为监控、安全合规性监控等,如分布式监控系统C...

fiddler抓包+雷电模拟器 完成手机app抓包的配置

找到系统应用,点击设置,点击无线网络WLAN—˃左键常按点击已连接网络—˃修改网络鼠标左键长按在桌面找到下面这个文件之后双击打开上面证书弄完之后。可以说本机已经安装过证书了,如果你能在模拟器上找到这个证书就不用将这个证书再拉入模拟器了在模拟器中打开系统应用—˃设置—˃安全—˃从SD卡安装。找到FiddlerRoot.cer文件,按提示导入即可,注意在此过程需...