2011-03-09 10 views
5

मैं निम्नलिखित पायथॉन लिपि विरासत में मिला हैउपयोगकर्ता पावरहेल स्क्रिप्ट यूआरएल पर पोस्ट करने के लिए?

import urllib2 
a = urllib2.urlopen('http://mysite/mypage.aspx?action=dosomething') 
a.read() 
a.close() 

और मैं एक powershell स्क्रिप्ट के साथ बदलना चाहते हैं। मैंने थोड़ा गुगल किया है लेकिन मुझे जो कुछ भी मिल रहा है वह एक ब्राउज़र विंडो लॉन्च करता है।

यह स्क्रिप्ट निर्धारित होने जा रही है, इसलिए यदि संभव हो तो मैं बस "पोस्ट और भूलना" चाहूंगा?

बहुत कृतज्ञता से प्राप्त किसी भी मदद :)

उत्तर

6

यहाँ एक स्क्रिप्ट मैं एक समय पहले लिखा था कि कैसे geotags के साथ ट्वीट पोस्ट करने के लिए पता चलता है कि है। यह वेब क्लाइंट का उपयोग करता है।

http://www.ravichaganti.com/blog/?p=979

आसान संदर्भ के लिए यहाँ कोड पेस्ट करना।

Function ByPass-Proxy { 
    param ([string]$url) 
    $webClient.Proxy.IsBypassed($url) 
} 

Function Get-GeoCoordinates { 
    param ([String]$location) 
    $baseURL = "http://maps.google.com/maps/geo?q=" 
    $apiKey = "Your API Key" 
    $url = $baseURL + $location + "&output=xml&sensor=false&key=" + $apiKey 
    $locCoords = (([xml]($WebClient.DownloadString($url))).kml.Response.Placemark.Point.coordinates) 
    return $locCoords 
} 

Function Send-Tweet { 
    param ([string]$Tweet,[string]$location) 
    $geoCoord = Get-GeoCoordinates $location 
    $long = $geoCoord.Split(",")[0] 
    $lat = $geoCoord.Split(",")[1] 
    $TwitURL = "http://twitter.com/statuses/update.xml" 
    $WebClient.Credentials = $TwitCredentials 
    #$str = [System.Text.Encoding]::UTF8.GetBytes("status=" + $Tweet + "&lat=" + $lat + "&long=" + $long) 
    $str = "status=" + $Tweet + "&lat=" + $lat + "&long=" + $long 
    [System.Net.ServicePointManager]::Expect100Continue = $false 
    $response = $WebClient.UploadString($TwitURL,$str) 
    $response 
} 

function Get-Credential { 
## Grabbed this from http://poshcode.org/1480 

[CmdletBinding(DefaultParameterSetName="Better")] 
PARAM(
    [Parameter(Position=1,Mandatory=$false)] 
    [Alias("Credential")] 
    [PSObject]$UserName=$null, 
    [Parameter(Position=2,Mandatory=$false)] 
    [string]$Title=$null, 
    [Parameter(Position=3,Mandatory=$false)] 
    [string]$Message=$null, 
    [Parameter(Position=4,Mandatory=$false)] 
    [string]$Domain=$null, 
    [Parameter(Mandatory=$false)] 
    [switch]$GenericCredentials, 
    [Parameter(Mandatory=$false)] 
    [switch]$Inline 
) 

PROCESS { 
    if($UserName -is [System.Management.Automation.PSCredential]) { 
     return $UserName 
    } elseif($UserName -ne $null) { 
     $UserName = $UserName.ToString() 
    } 

    if($Inline) { 
     if($Title) { Write-Host $Title } 
     if($Message) { Write-Host $Message } 
     if($Domain) { 
     if($UserName -and $UserName -notmatch "[@\\]") { 
      $UserName = "${Domain}\${UserName}" 
     } 
     } 
     if(!$UserName) { 
     $UserName = Read-Host "User" 
     if(($Domain -OR !$GenericCredentials) -and $UserName -notmatch "[@\\]") { 
      $UserName = "${Domain}\${UserName}" 
     } 
     } 
     return New-Object System.Management.Automation.PSCredential $UserName,$(Read-Host "Password for user $UserName" -AsSecureString) 
    } 
    if($GenericCredentials) { $Credential = "Generic" } else { $Credential = "Domain" } 

    ## Now call the Host.UI method ... if they don't have one, we'll die, yay. 
    ## BugBug? PowerShell.exe disregards the last parameter 
    $Host.UI.PromptForCredential($Title, $Message, $UserName, $Domain, $Credential,"Default") 
} 
} 

$global:webClient = New-Object System.Net.WebClient 
$global:TwitCredentials = Get-Credential -title "Twitter Credentials" -message "Please enter your Twitter username/password" 
If (!(ByPass-Proxy "http://www.twitter.com")) { 
    $global:Webclient.proxy.Credentials = Get-Credential -title "Proxy Credentials" -message "Please enter username/password for Proxy authentication" 
} 
0

आप Webclient वर्ग नेट से, विशेष रूप से UploadString या UploadValues उपयोग कर सकते हैं। एक वेब क्लाइंट ऑब्जेक्ट प्राप्त करने के लिए New-Object System.Net.WebClient करें, और बाकी को सीधा होना चाहिए।

6

फ़ंक्शन urlopen HTTP GET जैसा दिखता है। HTTP POST के लिए

$w = New-Object net.webclient 
$w.DownloadString('http://mysite/mypage.aspx?action=dosomething') 

मैं इस समारोह का उपयोग करें:: तो फिर तुम WebClient उपयोग कर सकते हैं

function Execute-HTTPPostCommand() 
{ 
    param(
    [string] $url = $null, 
    [string] $data = $null, 
    [System.Net.NetworkCredential]$credentials = $null, 
    [string] $contentType = "application/x-www-form-urlencoded", 
    [string] $codePageName = "UTF-8", 
    [string] $userAgent = $null 
); 

    if ($url -and $data) 
    { 
    [System.Net.WebRequest]$webRequest = [System.Net.WebRequest]::Create($url); 
    $webRequest.ServicePoint.Expect100Continue = $false; 
    if ($credentials) 
    { 
     $webRequest.Credentials = $credentials; 
     $webRequest.PreAuthenticate = $true; 
    } 
    $webRequest.ContentType = $contentType; 
    $webRequest.Method = "POST"; 
    if ($userAgent) 
    { 
     $webRequest.UserAgent = $userAgent; 
    } 

    $enc = [System.Text.Encoding]::GetEncoding($codePageName); 
    [byte[]]$bytes = $enc.GetBytes($data); 
    $webRequest.ContentLength = $bytes.Length; 
    [System.IO.Stream]$reqStream = $webRequest.GetRequestStream(); 
    $reqStream.Write($bytes, 0, $bytes.Length); 
    $reqStream.Flush(); 

    $resp = $webRequest.GetResponse(); 
    $rs = $resp.GetResponseStream(); 
    [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs; 
    $sr.ReadToEnd(); 
    } 
} 
6

PowerShell v3 के साथ शुरू, आप Invoke-WebRequest या उसके उर्फ ​​iwr उपयोग कर सकते हैं।

iwr 'http://mysite/mypage.aspx?action=dosomething' 

आप उत्पादन की जरूरत है, अजगर स्क्रिप्ट को एक ही प्रतिक्रिया की Content संपत्ति का उपयोग करें।

(Invoke-WebRequest 'http://mysite/mypage.aspx?action=dosomething').Content 

रूप @stej लिखा था, अजगर कोड किसी GET की तरह लग रहा है, लेकिन आप Method पैरामीटर का उपयोग कर सकते हैं अगर आप की क्या ज़रूरत है पोस्ट।

iwr 'http://mysite/mypage.aspx?action=dosomething' -Method Post 
संबंधित मुद्दे