【发布时间】:2016-08-26 00:36:46
【问题描述】:
我有一个在服务器上验证我的凭据的 url。有没有办法让它不可见?简单的代码看起来就像这样:
public void DoAuth()
{
String uri = GetUri();
ProcessStartInfo startInfo = new ProcessStartInfo(uri);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
}
但是ProcessWindowStyle.Hidden 似乎并不能解决问题。如果我将UseShellExecute 设置为false,那么我会得到Win32Exception 和消息The system cannot find the file specified
该 url 是 Spotify 服务器上用于获取播放列表的身份验证,它看起来像这样 https://accounts.spotify.com/authorize/client_id=26d287105as12315e12ds56e31491889f3cd293..
还有其他方法可以使这个过程不可见吗?
编辑:http 示例
public void DoAuth()
{
String uri = GetUri();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse webResponse;
try
{
webResponse = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Error code: {0}", webResponse.StatusCode);
using (Stream data = webResponse.GetResponseStream())
using (var reader = new StreamReader(data))
{
//do what here?
}
}
catch (Exception e)
{
throw;
}
}
包含上述示例的整个 .cs 文件:
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
namespace SpotifyAPI.Web.Auth
{
public class ImplicitGrantAuth
{
public delegate void OnResponseReceived(Token token, String state);
private SimpleHttpServer _httpServer;
private Thread _httpThread;
public String ClientId { get; set; }
public String RedirectUri { get; set; }
public String State { get; set; }
public Scope Scope { get; set; }
public Boolean ShowDialog { get; set; }
public event OnResponseReceived OnResponseReceivedEvent;
/// <summary>
/// Start the auth process (Make sure the internal HTTP-Server ist started)
/// </summary>
public void DoAuth()
{
String uri = GetUri();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse webResponse;
try
{
webResponse = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Error code: {0}", webResponse.StatusCode);
using (Stream data = webResponse.GetResponseStream())
using (var reader = new StreamReader(data))
{
//nothing
}
}
catch (Exception e)
{
throw;
}
/*ProcessStartInfo startInfo = new ProcessStartInfo(uri);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
*/
}
private String GetUri()
{
StringBuilder builder = new StringBuilder("https://accounts.spotify.com/authorize/?");
builder.Append("client_id=" + ClientId);
builder.Append("&response_type=token");
builder.Append("&redirect_uri=" + RedirectUri);
builder.Append("&state=" + State);
builder.Append("&scope=" + Scope.GetStringAttribute(" "));
builder.Append("&show_dialog=" + ShowDialog);
return builder.ToString();
}
/// <summary>
/// Start the internal HTTP-Server
/// </summary>
public void StartHttpServer(int port = 80)
{
_httpServer = new SimpleHttpServer(port, AuthType.Implicit);
_httpServer.OnAuth += HttpServerOnOnAuth;
_httpThread = new Thread(_httpServer.Listen);
_httpThread.Start();
}
private void HttpServerOnOnAuth(AuthEventArgs e)
{
OnResponseReceivedEvent?.Invoke(new Token
{
AccessToken = e.Code,
TokenType = e.TokenType,
ExpiresIn = e.ExpiresIn,
Error = e.Error
}, e.State);
}
/// <summary>
/// This will stop the internal HTTP-Server (Should be called after you got the Token)
/// </summary>
public void StopHttpServer()
{
_httpServer.Dispose();
_httpServer = null;
}
}
}
他们是这样称呼的:
_auth.OnResponseReceivedEvent += _auth_OnResponseReceivedEvent;
_auth.StartHttpServer(8000);
_auth.DoAuth();
带有完整可运行示例的 github url 在这里:https://github.com/JohnnyCrazy/SpotifyAPI-NET 下载它并运行 Spotify 测试以连接到 Spotify 的 web api 以重现我的示例。
【问题讨论】:
-
浏览器不是这样工作的;你不能那样做。您可能想要发送直接 HTTP 请求(但要注意 cookie)
-
@SLaks 我想过,但是什么样的要求?我应该如何处理响应?我不必在该请求上设置 cookie 吗?我将更新一个小样本,说明我如何理解你所说的
-
你想对响应做什么?这完全取决于您。
-
@SLaks 这就是问题所在。通常我对响应什么都不做。就连接完成而言,连接上有一个打开的套接字,我可以从 Spotify 获取播放列表。我会附上整个 .cs 文件,如果你有时间并且愿意的话,你可以检查整个代码
-
你在用什么样的项目?对于 .net 进程,您不必有任何 UI
标签: c# url process styles webclient