远程服务器返回错误:(401) 未经授权

发布于 2024-08-21 16:14:07 字数 1817 浏览 5 评论 0原文

我正在尝试在我的网站中嵌入批量短信 API ...但代码给出 401

ASPX :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" > 
<head id="Head1" runat="server"> 
  <title>Send SMS</title> 
</head> 
<body> 
  <form id="form2" runat="server"> 
    <div> 
    </div> 
  </form> 
</body> 
</html>

ASPX.CS

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Services;

public partial class _Default : System.Web.UI.Page 
{
     public void SendSMS()
    {
        UriBuilder urlBuilder = new UriBuilder();
        urlBuilder.Host = "IP";
        urlBuilder.Port = port;

        string UserName = "username";
        string password = "password";
        string PhoneNumber = "919999999999";
        string Text = "Testing";
        string Sender = "sender name";

        urlBuilder.Query = string.Format("user=" + UserName + "password=" + password + "PhoneNumber=" + PhoneNumber + "&Text=" + Text + "&Sender=" + Sender);

        HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(urlBuilder.ToString(), false));
        HttpWebResponse httpResponse = (HttpWebResponse)(httpReq.GetResponse());
    }



    protected void Page_Load(object sender, EventArgs e)
    {
        SendSMS();
    }

}

I m trying to embed Bulk SMS API in my website ...But the code is giving 401

ASPX :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" > 
<head id="Head1" runat="server"> 
  <title>Send SMS</title> 
</head> 
<body> 
  <form id="form2" runat="server"> 
    <div> 
    </div> 
  </form> 
</body> 
</html>

ASPX.CS

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Services;

public partial class _Default : System.Web.UI.Page 
{
     public void SendSMS()
    {
        UriBuilder urlBuilder = new UriBuilder();
        urlBuilder.Host = "IP";
        urlBuilder.Port = port;

        string UserName = "username";
        string password = "password";
        string PhoneNumber = "919999999999";
        string Text = "Testing";
        string Sender = "sender name";

        urlBuilder.Query = string.Format("user=" + UserName + "password=" + password + "PhoneNumber=" + PhoneNumber + "&Text=" + Text + "&Sender=" + Sender);

        HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(urlBuilder.ToString(), false));
        HttpWebResponse httpResponse = (HttpWebResponse)(httpReq.GetResponse());
    }



    protected void Page_Load(object sender, EventArgs e)
    {
        SendSMS();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洒一地阳光 2024-08-28 16:14:07

此代码:

    urlBuilder.Host = "IP";
    urlBuilder.Port = port;

告诉您的 HttpWebRequest 转到 http://IP:port,就像字面上的服务器 IP 一样。 http://IP 不存在,您需要将实际的主机名放在那里,例如“www.google.com”...无论您使用什么服务器正在尝试击中。

This code:

    urlBuilder.Host = "IP";
    urlBuilder.Port = port;

Is telling your HttpWebRequest to go to http://IP:port as in literally the server IP. http://IP doesn't exist, you need to put the actual hostname there, like "www.google.com"...whatever server you're trying to hit.

迟到的我 2024-08-28 16:14:07

问题很可能在于:

urlBuilder.Query = string.Format("user=" + UserName + "password=" + password + "PhoneNumber=" + PhoneNumber + "&Text=" + Text + "&Sender=" + Sender);

您缺少一些 &s。可能应该是这样的:

urlBuilder.Query = string.Format("user=" + UserName + "&password=" + password + "&PhoneNumber=" + PhoneNumber + "&Text=" + Text + "&Sender=" + Sender); 

编辑:也没有理由在这里使用 string.Format() 。这会起到同样的作用:

urlBuilder.Query = "user=" + UserName + "&password=" + password + "&PhoneNumber=" + PhoneNumber + "&Text=" + Text + "&Sender=" + Sender; 

The problem is most likely in:

urlBuilder.Query = string.Format("user=" + UserName + "password=" + password + "PhoneNumber=" + PhoneNumber + "&Text=" + Text + "&Sender=" + Sender);

You're missing a few &s. Should probably be something like:

urlBuilder.Query = string.Format("user=" + UserName + "&password=" + password + "&PhoneNumber=" + PhoneNumber + "&Text=" + Text + "&Sender=" + Sender); 

Edit: Also there is no reason to be using string.Format() here. This would work the same:

urlBuilder.Query = "user=" + UserName + "&password=" + password + "&PhoneNumber=" + PhoneNumber + "&Text=" + Text + "&Sender=" + Sender; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文