Здравствуйте. Подскажите как правильно отправить через пост запрос какой-то текст и получить ответ. Использую Web service C#. Также как можно получить Ip адрес того, кто отправил пост запрос.
В клиентском приложении
возникает ошибка The remote server returned an error: (500) Internal Server Error.
в строке WebResponse res = req.GetResponse();
Код сервиса :
using System.Web.Services;
namespace WebService1
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(string text)
{
return "Hello World " + text;
}
}
}
Код клиента с пост запросом:
namespace Examples.System.Net
{
public class WebRequestPostExample
{
public static void Main()
{
Console.WriteLine(POST("http://localhost:11364/Service1.asmx/HelloWorld", "lolka"));
Console.ReadLine();
}
private static string POST(string Url, string Data)
{
WebRequest req = WebRequest.Create(Url);
req.Method = "POST";
req.Timeout = 100000;
req.ContentType = "application/x-www-form-urlencoded";
byte[] sentData = Encoding.GetEncoding(1251).GetBytes(Data);
req.ContentLength = sentData.Length;
Stream sendStream = req.GetRequestStream();
sendStream.Write(sentData, 0, sentData.Length);
sendStream.Close();
WebResponse res = req.GetResponse();
Stream ReceiveStream = res.GetResponseStream();
StreamReader sr = new StreamReader(ReceiveStream, Encoding.UTF8);
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
string Out = String.Empty;
while (count > 0)
{
String str = new String(read, 0, count);
Out += str;
count = sr.Read(read, 0, 256);
}
return Out;
}
}
Вот что требует веб сервис мой на странице тестирования
HTTP POST
The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.
POST /Service1.asmx/HelloWorld HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
text=string