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

时间:2010-02-14 15:35:50

标签: c# asp.net api sms bulksms

我正在尝试在我的网站中嵌入Bulk SMS 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();
    }

}

2 个答案:

答案 0 :(得分:1)

此代码:

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

告诉您的HttpWebRequest转到http://IP:port,就像服务器IP一样。 http://IP不存在,您需要在其中输入实际的主机名,例如“www.google.com”...您要尝试的任何服务器。

答案 1 :(得分:0)

问题最有可能发生在:

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

你错过了一些&amp; 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;