HTTP POST in .NET doesn't work

I've got a problem with creating an HTTP post request in .NET. When I do this request in ruby it does work.

When doing the request in .NET I get following error:

<h1>FOXISAPI call failed</h1><p><b>Progid is:</b> carejobs.carejobs
<p><b>Method is:</b> importvacature/
<p><b>Parameters are:</b> 
<p><b> parameters are:</b> vacature.deelnemernr=478
</b><p><b>GetIDsOfNames failed with err code 80020006: Unknown name.
</b>

Does anyone knows how to fix this?

Ruby:

require 'net/http'

url = URI.parse('http://www.carejobs.be/scripts/foxisapi.dll/carejobs.carejobs.importvacature')

post_args = {
  'vacature.deelnemernr' => '478',
}

resp, data = Net::HTTP.post_form(url, post_args)

print resp
print data

C#:

Uri address = new Uri(url);  
// Create the web request  
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;  
// Set type to POST  
request.Method = "POST";  
request.ContentType = "application/x-www-form-urlencoded";
// Create the data we want to send
StringBuilder data = new StringBuilder();  
data.Append("vacature.deelnemernr=" + HttpUtility.UrlEncode("478"));
// Create a byte array of the data we want to send  
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());  
// Set the content length in the request headers  
request.ContentLength = byteData.Length;  
// Write data  
using (Stream postStream = request.GetRequestStream())  
{  
    postStream.Write(byteData, 0, byteData.Length);  
}  
// Get response  
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)  
{
    // Get the response stream
    StreamReader reader = new StreamReader(response.GetResponseStream());  
    // Console application output  
    result = reader.ReadToEnd();
}
return result;

Don't you need the ? after the URL in order to do a post with parameters? I think that Ruby hides this behind the scenes.


I found the problem! The url variable in the C# code was "http://www.carejobs.be/scripts/foxisapi.dll/carejobs.carejobs.importvacature/" It had to be "http://www.carejobs.be/scripts/foxisapi.dll/carejobs.carejobs.importvacature" without the backslash.

链接地址: http://www.djcxy.com/p/8012.html

上一篇: 使用web响应对象获取(500)内部服务器错误

下一篇: .NET中的HTTP POST不起作用