相关疑难解决方法(0)

使用HttpWebRequest发布表单数据

我想将一些表单数据发布到不在我自己的Web应用程序中的指定URL.它具有相同的域,例如"domain.client.nl".Web应用程序有一个URL"web.domain.client.nl",我要发布到的URL是"idp.domain.client.nl".但我的代码什么也没做.....有人知道我做错了吗?

沃特

StringBuilder postData = new StringBuilder();
postData.Append(HttpUtility.UrlEncode(String.Format("username={0}&", uname)));
postData.Append(HttpUtility.UrlEncode(String.Format("password={0}&", pword)));
postData.Append(HttpUtility.UrlEncode(String.Format("url_success={0}&", urlSuccess)));
postData.Append(HttpUtility.UrlEncode(String.Format("url_failed={0}", urlFailed)));

ASCIIEncoding ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData.ToString());

// set up request object
HttpWebRequest request;
try
{
    request = (HttpWebRequest)HttpWebRequest.Create(WebSiteConstants.UrlIdp);
}
catch (UriFormatException)
{
    request = null;
}
if (request == null)
    throw new ApplicationException("Invalid URL: " + WebSiteConstants.UrlIdp);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; …
Run Code Online (Sandbox Code Playgroud)

c# post httpwebrequest

84
推荐指数
3
解决办法
20万
查看次数

C#HttpWebRequest类型为"application/x-www-form-urlencoded" - 如何在内容体中发送'&'字符?

我正在用C#编写一个API连接的小应用程序.

我连接到一个API,它有一个采用长字符串的方法,即日历(ics)文件的内容.

我是这样做的:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = "POST";
request.AllowAutoRedirect = false;
request.CookieContainer = my_cookie_container;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.ContentType = "application/x-www-form-urlencoded";

string iCalStr = GetCalendarAsString();

string strNew = "&uploadfile=true&file=" + iCalStr;

using (StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII))
 {
     stOut.Write(strNew);
     stOut.Close();
 }
Run Code Online (Sandbox Code Playgroud)

这似乎很有效,直到我在我的日历中添加一些特定的HTML.

如果我在我的日历(或类似)中的某个地方有一个' ',那么服务器只会获得所有数据到'&' - 点,所以我假设'&'使得它看起来像这个点之后的任何东西属于一个新参数?

我怎样才能解决这个问题?

c# post httpwebrequest

32
推荐指数
3
解决办法
8万
查看次数

WebApi Post 方法总是返回“请求的资源不支持 http 方法 'GET'。” 状态:405 方法不允许

我创建了一个简单的 web api 服务来获取和发布用户数据

Localhost 的一切都很好。但是当我在服务器上托管 Service 时,当我从 PostMan/Browser 调用它时,Get Method 工作正常。但是 Post Methods 总是返回“请求的资源不支持 http 方法 'GET'。” 状态:405 方法不允许

我在这里感到困惑的一件事,即我请求了一个 POST 呼叫,但状态消息显示我“GET”错误。为什么应该这样?如果是CORS问题?我通过在应用程序级别(Web.Config 以及 Nuget 包管理器 Cors)在 Internet 上搜索答案,尝试在各种场景/方面启用 CORS。仍然得到 405 Method Not Allowed。下面粘贴了我的 API 代码:

控制器命名空间:

  using MySql.Data.MySqlClient;
  using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  using System.Web.Http.Cors;
Run Code Online (Sandbox Code Playgroud)

控制器

  public class UsersController : ApiController
  {
    [Route("api/Users/GetUsers/{UserId}")]
    [HttpGet]
    public IEnumerable<User> GetUsers(int UserId)
    {
        try
        {
            List<User> userlist = new List<User>(); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api asp.net-web-api2

5
推荐指数
2
解决办法
1万
查看次数

如何删除AspxAutoDetectCookieSupport = 1

我有一个网址,如http://www.foo.com/NewPage.aspx?pageid=10.但是对某些用户来说,这会显示为http://www.foo.com/NewPage.aspx?pageid = 10 &

现在我读到AspxAutoDetectCookieSupport = 1被添加到我的web.config中,因为我有我的web.config,<sessionState cookieless="AutoDetect"/>而它应该是<sessionState cookieless="UseCookies"/>

我想知道的是,这个改变是否存在问题.

asp.net

4
推荐指数
1
解决办法
2万
查看次数