代码之家  ›  专栏  ›  技术社区  ›  tom

.NET中的HTTP Post不起作用

  •  0
  • tom  · 技术社区  · 15 年前

    我在.NET中创建HTTP Post请求时遇到问题。当我在Ruby中执行这个请求时,它确实有效。

    在.NET中执行请求时,出现以下错误:

    <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>
    

    有人知道怎么解决这个问题吗?

    红宝石:

    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;
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   TheCodeMonk    15 年前

    你不需要吗?在URL之后要用参数做一个帖子吗?我想鲁比把这个藏在幕后。

        2
  •  -1
  •   tom    15 年前