我有一个类似Java的Servelet http://152.252.271.12:9999/media/servlet/RequestProcessor.do
为此,我需要像这样在Querystring中传递参数 http://152.252.271.12:9999/media/servlet/RequestProcessor.do?input=abc
我需要阅读回复。
我试过这样。
request.Method = "POST";
request.KeepAlive = false;
request.Credentials = CredentialCache.DefaultCredentials;
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(postData);
}
// Read Response.
HttpWebResponse webresponse = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(webresponse.GetResponseStream(), Encoding.Default))
{
result = reader.ReadToEnd();
logtxn.LogTxn(result, "SPDCL RESPONSE");
}
webresponse.Close();
任何帮助表示赞赏..
答案 0 :(得分:0)
使用WebRequest& WebResponse类。查看http://gsraj.tripod.com/dotnet/webgetpost.html了解详情。
答案 1 :(得分:0)
我过去遇到过类似的问题,我不得不在IIS中托管一个服务,该服务接受包含请求的字符串,并返回另一个字符串,其中包含对该请求的响应。所有上述操作都需要使用HTTP POST完成。
我找到的解决方案是在IIS上托管WCF服务并将其配置为使用HTTP GET / POST协议。
另外我记得我需要使用Stream类来输入和输出数据(在这种情况下,一个简单的字符串不起作用),换句话说,我在WCF中有一个HTTP POST方法,如:
Stream GetData(Stream data);
看看这个article。
答案 2 :(得分:0)
您可以使用WebClient
类作为servlet的客户端。例如
WebClient myWebClient = new WebClient();
// Download the Web resource and save it into a data buffer.
byte[] myDataBuffer = myWebClient.DownloadData (@"http://152.252.271.12:9999/media/servlet/RequestProcessor.do?input=abc");
// Display the downloaded data.
string download = Encoding.ASCII.GetString(myDataBuffer);
现在解析结果字符串download
并获取响应。
答案 3 :(得分:0)
请找到以下代码:
public class MyWebRequest
{
private WebRequest request;
private Stream dataStream;
private string status;
public String Status
{
get
{
return status;
}
set
{
status = value;
}
}
public MyWebRequest(string url)
{
// Create a request using a URL that can receive a post.
request = WebRequest.Create(url);
}
public MyWebRequest(string url, string method): this(url)
{
if (method.Equals("GET") || method.Equals("POST"))
{
// Set the Method property of the request to POST.
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
}
public MyWebRequest(string url, string method, string data): this(url, method)
{
// Create POST data and convert it to a byte array.
string postData = data;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
}
public string GetResponse()
{
// Get the original response.
WebResponse response = request.GetResponse();
this.Status = ((HttpWebResponse) response)
.StatusDescription;
// Get the stream containing all content returned by the requested server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content fully up to the end.
string responseFromServer = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
}
//create the constructor with post type and few data
MyWebRequest myRequest = new MyWebRequest("http://www.yourdomain.com", "POST", "a=value1&b=value2");
//show the response string on the console screen.
Console.WriteLine(myRequest.GetResponse());