问题描述
我想用HTTPwebrequest来调用webservice。
我试过这个方法吧不工作,可以帮助我解决这个问题。
先谢谢。
public static string fnSendandReceiveurlencode(string Request,string WebServiceURL)
{
string rawResponse;
HttpWebRequest webreq =(HttpWebRequest)WebRequest.Create(WebServiceURL);
webreq.ContentType =text / xml; charset = utf-8;
//webreq.Accept =text / xml;
webreq.Headers.Clear();
webreq.Method =POST;
byte [] bytes = Encoding.UTF8.GetBytes(Request.ToString());
Encoding encode = Encoding.GetEncoding(utf-8);
webreq.ContentLength = bytes.Length;
HttpWebResponse webres = null;
webres =(HttpWebResponse)webreq.GetResponse();
流阅读器= null;
reader = webres.GetResponseStream();
StreamReader sreader = new StreamReader(reader,encode,true);
rawResponse = sreader.ReadToEnd();
返回rawResponse;
}
你的
Sheethal
Hi,
I want to call webservice using HTTPwebrequest.
I tried this method it is not working, can any help me to solve this issue.
Thanks in Advance.
public static string fnSendandReceiveurlencode(string Request, string WebServiceURL)
{
string rawResponse;
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(WebServiceURL);
webreq.ContentType = "text/xml; charset=utf-8";
//webreq.Accept = "text/xml";
webreq.Headers.Clear();
webreq.Method = "POST";
byte[] bytes = Encoding.UTF8.GetBytes(Request.ToString());
Encoding encode = Encoding.GetEncoding("utf-8");
webreq.ContentLength = bytes.Length;
HttpWebResponse webres = null;
webres = (HttpWebResponse)webreq.GetResponse();
Stream reader = null;
reader = webres.GetResponseStream();
StreamReader sreader = new StreamReader(reader, encode, true);
rawResponse = sreader.ReadToEnd();
return rawResponse;
}
Yours
Sheethal
推荐答案
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(WebServiceURL);
webreq.Method = "POST";
webreq.ContentType = "text/xml";
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(Request.ToString());
webreq.ContentLength = bytes.Length;
using (var writeStream = webreq.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
using (var response = (HttpWebResponse)webreq.GetResponse())
{
var responseValue = string.Empty;
if(response.StatusCode == HttpStatusCode.OK)
{
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
}
}
这篇关于使用HTTPwebrequest方法调用webservice,输入参数为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!