如何使用HttpWebRequest类将来自C#的Web服务的发布数据发送到Web应用程序? [英] How can I send a post data from a web service in C# to a web application using a HttpWebRequest class ?

查看:52
本文介绍了如何使用HttpWebRequest类将来自C#的Web服务的发布数据发送到Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码



[WebMethod]

公共字符串GetResponsePOSTData()

{

string valueReturn = string.Empty;

try

{

//我们的postvars

string dataToPost =username = username&password = password;

byte [] buffer = Encoding.ASCII.GetBytes(dataToPost);

//初始化,我们使用localhost,如果适用则更改

HttpWebRequest WebReq =(HttpWebRequest)WebRequest.Create(http:// localhost:8081 / testSubmit.php);



//我们的方法是post,否则缓冲区(postvars)将无用

WebReq.Method =POST;



//我们对postvars使用form contentType。

//WebReq.ContentType =application / x-www-form-urlencoded;

WebReq.ContentType =text / xml;

//缓冲区的长度(postvars)用作contentlength。

WebReq.ContentLength = buffer.Length; < br $> b $ b

//我们打开一个用于编写postvars的流

Stream PostData = WebReq.GetRequestStream();

//现在我们写,然后,我们关闭。结束总是很重要!

PostData.Write(buffer,0,buffer.Length);

PostData.Close();



//获取响应句柄,我们还没有真正的回复!

HttpWebResponse WebResp =(HttpWebResponse)WebReq.GetResponse();



//现在,我们读取响应(字符串)并输出它。

Stream Answer = WebResp.GetResponseStream();

StreamReader _Answer = new StreamReader(Answer);

valueReturn = _Answer.ReadToEnd()。ToString();

}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}



返回值返回;

}



但它返回:



此XML hav似乎没有任何与之相关的风格信息。文档树如下所示。



它必须添加参数[WebMethod]或在Web.Config文件中进行配置???

解决方案

参见 http://forums.asp.net/t/1799042.aspx?How+to+call+WebMethod+of+a+Web+service+using+HttpWebRequest+object+ [ ^ ]

I use this code

[WebMethod]
public string GetResponsePOSTData()
{
string valueReturn = string.Empty;
try
{
//Our postvars
string dataToPost = "username=username&password=password";
byte[] buffer = Encoding.ASCII.GetBytes(dataToPost);
//Initialization, we use localhost, change if applicable
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://localhost:8081/testSubmit.php");

//Our method is post, otherwise the buffer (postvars) would be useless
WebReq.Method = "POST";

//We use form contentType, for the postvars.
//WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentType = "text/xml";
//The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length;

//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();

//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
valueReturn = _Answer.ReadToEnd().ToString();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}

return valueReturn;
}

but it returns :

This XML file does not appear to have any style information associated with it. The document tree is shown below.

it must add arguments [WebMethod] or make a configuration in the Web.Config file ???

解决方案

See http://forums.asp.net/t/1799042.aspx?How+to+call+WebMethod+of+a+Web+service+using+HttpWebRequest+object+[^]


这篇关于如何使用HttpWebRequest类将来自C#的Web服务的发布数据发送到Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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