asp.net 如何调用http接口
asp.net采用post传递数据到远程其他系统的http接口。
------解决方案--------------------msdn search
httpwebrequest
------解决方案--------------------HttpWebRequest
------解决方案--------------------WebRequest req = null;
WebResponse rsp = null;
try
{
string uri = "http://xxx ";
req = WebRequest.Create(uri);
//req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
req.Method = "POST "; // Post method
req.ContentType = "text/xml "; // content type
// Wrap the request stream with a text-based writer
StreamWriter writer = new StreamWriter(req.GetRequestStream());
// Write the xml text into the stream
writer.WriteLine( "ASDFASD ");
writer.Close();
// Send the data to the webserver
rsp = req.GetResponse();
}
catch(WebException webEx)
{
string dd=webEx.Message;
}
catch(Exception ex)
{
string dd=ex.Message;
}
finally
{
if(req != null) req.GetRequestStream().Close();
if(rsp != null) rsp.GetResponseStream().Close();
}