2010-02-14 225 views
1

餘米試圖嵌入批量短信API在我的網站...但代碼是給401遠程服務器返回錯誤:(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(); 
    } 

} 
+0

IP,端口,用戶名,密碼,9999999999,發送者姓名 都換人.. – 2010-02-14 16:01:48

回答

1

此代碼:

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

我告訴你的HttpWebRequest去到http://IP:port,就像字面上的服務器IP一樣。 http://IP不存在,你需要把實際的主機名放在那裏,比如「www.google.com」......你試圖打的任何服務器。

+0

我代我的服務器的實際IP有代替IP字... – 2010-02-14 15:58:35

+0

@KK - 所以沒有路徑?只需'http:// host:port /?user = usernamepassword = passwordPhoneNumber = 91999999999&Text = Testing&Sender = sender%20name'?注意:你可能想在你的查詢參數之間增加一些'&'。 – 2010-02-14 16:04:25

+0

ohh狗屎..我忘了在2個地點Thanx男人...我浪費了很多時間..再次.. .. – 2010-02-14 16:13:37

0

的問題很可能出在:

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

你錯過了幾個&秒。應該可能是這樣的:

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; 
相關問題