如何通过HTTP请求到Web服务 [英] How to pass HTTP Request to web service

查看:140
本文介绍了如何通过HTTP请求到Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IDE:VS 2010

我有我必须使用来自HTTP请求服务(如果没有客户端项目添加服务引用)。我按照教程很多的要求,但在所有我得到错误505,同时使

  System.Net.HttpWebResponse RESP =(System.Net.HttpWebResponse)req.GetResponse();

下面是示例code,我使用了一个

  System.Net.WebRequest REQ = System.Net.WebRequest.Create(HTTP://本地主机:8090 / MyService.asmx);
        //req.Proxy =新System.Net.WebProxy(127.0.0.1,真正的);
        //req.Htt(\"POST /_22YardzWebService.asmx/ReceiveXMLByContent HTTP / 1.1);
        //builder.Append(\"Host:本地主机);
        req.Co​​ntentType =文本/ XML;编码='UTF-8';
        //req.Co​​ntentLength = 4096;        //添加这些,因为我们正在做一个POST
        //req.Co​​ntentType =应用/的X WWW的形式urlen codeD;        req.Method =POST;
        //我们需要计算我们有多少字节发送。 Post'ed伪造的表格应为name = value&安培;
        字节[]字节= Encoding.UTF8.GetBytes(参数);
        req.Co​​ntentLength = bytes.Length;
        //System.IO.Stream OS = req.GetRequestStream();
         //推它在那里        使用(流OS = req.GetRequestStream())
        {
            os.Write(字节,0,bytes.Length);
        }
        //os.Close();        System.Net.HttpWebResponse RESP =(System.Net.HttpWebResponse)req.GetResponse();
        如果(RESP == NULL)返回NULL;
        就是System.IO.StreamReader SR =新就是System.IO.StreamReader(resp.GetResponseStream());
        返回sr.ReadToEnd()修剪()。

code来源:的http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx

Web方法是从工作服务引用调用时,

样本Web马托:

  [的WebMethod]
    公共字符串测试()
    {        返回你好;
    }


解决方案

您抽样方法不返回数据。

下面是我的榜样和工作。

  [WebService的空间(namespace =htt​​p://tempuri.org/)]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
    [System.ComponentModel.ToolboxItem(假)]
    公共类服务1:System.Web.Services.WebService
    {        [的WebMethod]
        公共字符串的HelloWorld()
        {
            返回的Hello World;
        }        [的WebMethod]
         公共字符串HelloWorldNew(字符串数据1,字符串数据2)
         {
          返回的Hello World+数据1 +数据2;
         }
    }

控制台应用程序code。 (简单的HelloWorld)

 类节目
{
    静态无效的主要(字串[] args)
    {
        HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(HTTP://本地主机:60395 / Service1.asmx的/的HelloWorld);
        request.Method =POST;
        VAR读卡器=新就是System.IO.StreamReader(request.GetResponse()GetResponseStream());
        字符串数据= reader.ReadToEnd();
    }
}

控制台应用程序code。 (简单HelloWorldNew)

 类节目
{
    静态无效的主要(字串[] args)
    {
        字符串数据=数据1 = USER1和放大器;数据2 =用户2;
        字节[]数据流= Encoding.UTF8.GetBytes(数据);
        HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(HTTP://本地主机:60395 / Service1.asmx的/ HelloWorldNew);
        request.Method =POST;
        request.ContentType =应用/的X WWW的形式urlen codeD;
        request.ContentLength = dataStream.Length;
        方通流= request.GetRequestStream();
        newStream.Write(数据流,0,dataStream.Length);
        newStream.Close();
        VAR读卡器=新就是System.IO.StreamReader(request.GetResponse()GetResponseStream());
        字符串dataReturn = reader.ReadToEnd();
    }
}

IDE: VS 2010

I have a requirement in that I have to consume service from HTTP request (Without adding service reference in client project.) I have followed lots of tutorials, but in all I am getting error 505, while making

System.Net.HttpWebResponse resp =(System.Net.HttpWebResponse) req.GetResponse();

Here is one of the sample code which I have used

        System.Net.WebRequest req = System.Net.WebRequest.Create("http://localhost:8090/MyService.asmx");
        //req.Proxy = new System.Net.WebProxy("127.0.0.1", true);
        //req.Htt("POST /_22YardzWebService.asmx/ReceiveXMLByContent HTTP/1.1");
        //builder.Append("Host: localhost");
        req.ContentType = "text/xml; encoding='utf-8'";
        //req.ContentLength = 4096;

        //Add these, as we're doing a POST
        //req.ContentType = "application/x-www-form-urlencoded";

        req.Method = "POST";
        //We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
        byte[] bytes = Encoding.UTF8.GetBytes(Parameters);
        req.ContentLength = bytes.Length;


        //System.IO.Stream os = req.GetRequestStream();
         //Push it out there

        using (Stream os = req.GetRequestStream())
        {
            os.Write(bytes, 0, bytes.Length);
        }
        //os.Close();

        System.Net.HttpWebResponse resp =(System.Net.HttpWebResponse) req.GetResponse();
        if (resp == null) return null;
        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        return sr.ReadToEnd().Trim();  

code source :http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx

Web method is working when called from service reference,

sample web Mathod:

[WebMethod]
    public string Test()
    {

        return "hello";
    }

解决方案

Your sample method does not return data.

Here is my example and working.

[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
         public string HelloWorldNew(string data1,string data2)
         {
          return "Hello World" + data1 + data2;
         } 
    }

Console Application Code. ( Simple HelloWorld)

class Program
{
    static void Main(string[] args)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:60395/Service1.asmx/HelloWorld");
        request.Method = "POST";
        var reader = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
        string data = reader.ReadToEnd();
    }
}

Console Application Code. ( Simple HelloWorldNew)

class Program
{
    static void Main(string[] args)
    {
        string data = "data1=user1&data2=user2";
        byte[] dataStream = Encoding.UTF8.GetBytes(data);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:60395/Service1.asmx/HelloWorldNew");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = dataStream.Length;
        Stream newStream = request.GetRequestStream();            
        newStream.Write(dataStream, 0, dataStream.Length);
        newStream.Close();
        var reader = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
        string dataReturn = reader.ReadToEnd();
    }
}  

这篇关于如何通过HTTP请求到Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆