使用 HttpWebRequest 类 [英] Using the HttpWebRequest class

查看:17
本文介绍了使用 HttpWebRequest 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实例化了 HttpWebRequest 对象:

I instantiate the HttpWebRequest object:

HttpWebRequest httpWebRequest = 
    WebRequest.Create("http://game.stop.com/webservice/services/gameup")
    as HttpWebRequest;

当我将数据发布"到此服务时,该服务如何知道将数据提交到哪个 Web 方法?

When I "post" the data to this service, how does the service know which web method to submit the data to?

我没有这个网络服务的代码,我只知道它是用 Java 编写的.

I do not have the code to this web service, all I know is that it was written in Java.

推荐答案

这有点复杂,但完全可行.

This gets a bit complicated but it's perfectly doable.

您必须知道要执行的 SOAPAction.如果你不这样做,你就不能提出请求.如果您不想手动设置此项,您可以向 Visual Studio 添加服务引用,但您需要知道服务端点.

You have to know the SOAPAction you want to take. If you don't you can't make the request. If you don't want to set this up manually you can add a service reference to Visual Studio but you will need to know the services endpoint.

以下代码用于手动 SOAP 请求.

The code below is for a manual SOAP request.

// load that XML that you want to post
// it doesn't have to load from an XML doc, this is just
// how we do it
XmlDocument doc = new XmlDocument();
doc.Load( Server.MapPath( "some_file.xml" ) );

// create the request to your URL
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( Your URL );

// add the headers
// the SOAPACtion determines what action the web service should use
// YOU MUST KNOW THIS and SET IT HERE
request.Headers.Add( "SOAPAction", YOUR SOAP ACTION );

// set the request type
// we user utf-8 but set the content type here
request.ContentType = "text/xml;charset="utf-8"";
request.Accept = "text/xml";
request.Method = "POST";

// add our body to the request
Stream stream = request.GetRequestStream();
doc.Save( stream );
stream.Close();

// get the response back
using( HttpWebResponse response = (HttpWebResponse)request.GetResponse() )
{
     // do something with the response here
}//end using

这篇关于使用 HttpWebRequest 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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